小编Sai*_*ala的帖子

当从同一个类的公共方法返回时,为什么我能够在其他类的公共方法中修改私有成员

我能够从其他类的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)

c# access-modifiers private-members c#-7.0

0
推荐指数
1
解决办法
200
查看次数

标签 统计

access-modifiers ×1

c# ×1

c#-7.0 ×1

private-members ×1