• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
拿一份干净工资,过一个简单生活
c#——最优雅的语言
博客园    首页    新随笔    联系   管理    订阅  订阅
c#函数的三个优雅的参数修饰符
其实这三个关键词大家都在用,很简单,我把它们当做自己的第一篇小随笔记录下来,是想说明自己的副标题的,c#确实一门很优雅的语言。
1、ref参数修饰符,其作用是创建引用调用而不是值调用,直接看例子
using System;
//ref
class refclass
{
  
public void chg(ref int i,ref int j)
 {
    i
++;
    j
++
  }
}

class refdemo
{
  
public static Main()
  {
    refclass rc
=new refclass();
    
int a=100;
    
int b=100;
    rc.chg(
ref a,ref b);
    Console.WriteLine(a,b);
   }
}


using System;
//out
class outclass
{
  
public void chg(out int i,out int j)
 {
    i
++;
    j
=i+100;
  }
}

class outdemo
{
  
public static Main()
  {
    outclass oc
=new outclass();
    
int a=100;
    
int b;
    oc.chg(
out a,out b);
    Console.WriteLine(a,b);
   }
}



其实我们通常用这种方式来返回函数的多个值,以前想返回多个值还要自己去组装数据什么的,现在就不用了,一个字“爽”!
2、out参数修饰符和ref差不多,不同的是它不需要初始化,但记住申明是要的.
3、params参数修饰符,可以在函数调用时传送个数不定的参数。[由于第一次写东西,不知怎么贴代码,就不贴在上面了]
using System;
class max
{
  public int max(params int[] nums)
  {
    int k=nums[0];
    for(int i=1;i<nums.Length;i++)
    {
      if(nums[i]>k)
         k=nums[i];    
    }
    return k;
  }
}

class paramsdemo
{
  public static void Main()
  {
    max mx=new max();
    int max;
    int a=10,b=5,c=6;
    max=mx.max(a,b,c);//调用三个参数
    Cosole.WriteLine(max); 

    int d=20;
    max=mx.max(a,b,c,d);//调用四个参数
    Cosole.WriteLine(max); 

  }
}

posted on 2006-11-20 00:02  老头  阅读(463)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3