ref 和out 关键字
static void Main(string[] args)
{
int a = 3;
int b = 6;
//ref 有进有出 , out 只出不进
Console.WriteLine(Fun(ref a, ref b));
Console.WriteLine(Fun2(out a, out b));
Console.ReadLine();
}
//ref 在方法里面如果有声明形参的值,就用方法里面的变量的值;没有的话就用实际变量的值,并且在调用的时候必须初始化实参的值
static int Fun(ref int x, ref int y)
{
x = 15;
// y = 2;
return x + y;
}
//out 必须在方法里面初始化形参的值,并且在调用的时候,不管实参是否有值还是没值都是无效的 。
static int Fun2(out int x, out int y)
{
x = 15;
y = 2;
return x + y;
}
{
int a = 3;
int b = 6;
//ref 有进有出 , out 只出不进
Console.WriteLine(Fun(ref a, ref b));
Console.WriteLine(Fun2(out a, out b));
Console.ReadLine();
}
//ref 在方法里面如果有声明形参的值,就用方法里面的变量的值;没有的话就用实际变量的值,并且在调用的时候必须初始化实参的值
static int Fun(ref int x, ref int y)
{
x = 15;
// y = 2;
return x + y;
}
//out 必须在方法里面初始化形参的值,并且在调用的时候,不管实参是否有值还是没值都是无效的 。
static int Fun2(out int x, out int y)
{
x = 15;
y = 2;
return x + y;
}


浙公网安备 33010602011771号