|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
引用参数的行为就像是将实参作为形参的别名,这样子就容易理解是怎么传递的了。
namespace 引用类型作为引用参数传递实例
{
class Program
{
static void Main(string[] args)
{
MyClass a1 = new MyClass(); //实例化
Console.WriteLine("Before method call: {0}", a1.Val);
RefAsParameter(ref a1); //引用参数调用
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
|
评分
-
查看全部评分
|