C#入门基础——方法-引用参数(引用类型)
引用参数的行为就像是将实参作为形参的别名,这样子就容易理解是怎么传递的了。namespace 引用类型作为引用参数传递实例
{
class Program
{
static void Main(string[] args)
{
MyClass a1 = new MyClass(); //实例化
Console.WriteLine("Before method call: {0}", a1.Val);
RefAsParameter(refa1); //引用参数调用
Console.WriteLine("After method call: {0}", a1.Val);
Console.ReadKey();
}
static void RefAsParameter(ref MyClass f1) //静态方法-引用参数
{
f1.Val = 50;
Console.WriteLine("After member assignment: {0}", f1.Val);
f1 = new MyClass(); //实例化
Console.WriteLine("After new object creation: {0}", f1.Val);
}
class MyClass
{
public int Val = 20;
}
}
}
输出结果:
Before method call: 20
After member assignment:50
After new object creation: 20
After method call: 20
{:7_136:}
页:
[1]