我能够从其他类的main方法更新类实例的私有成员.为什么我被允许这样做.它是否喜欢,它使用"ref"关键字?
我尝试使用"ref"关键字进行修改.
using System;
namespace Test2
{
class A
{
private int a=10;
public ref int M()
{
return ref a;
}
virtual public void display()
{
Console.WriteLine("class A");
}
}
class B:A
{
}
class Program
{
static void Main(string[] args)
{
B b = new B();
ref int a = ref b.M();
Console.WriteLine(a);
a = 20;
Console.WriteLine(b.M());
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)