兰冰点点

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ref和out关键字都为引用类型,使用后都会修改实参的值

class Program
{
static void Main(string[] args)
{
int m = 10;
int n;


GetInt(ref m);
GetInt2(out n)
;
Console.WriteLine("m={0}", m);
Console.WriteLine("n={0}", n);


Console.ReadKey();
}


static void GetInt(ref int a)
{
a = a + 1;
}
static void GetInt2(out int b)
{
b = 1;
b = b + 1;

}

 

示例说明二者的区别:

1、ref和out进行使用时,形参和实参都必须用该关键字定义

2、out实参不需要初始化,ref实参需要先初始化

3、方法内如果需要使用out的形参,那么形参必须先初始化

posted on 2021-04-15 10:24  兰冰点点  阅读(159)  评论(0编辑  收藏  举报