我遇到了一种情况,我可以在外面修改班级的私人成员。这是我的课:
public class MyClass
{
private List<string> _myClassMember = null;
public MyClass()
{
_myClassMember = new List<string>();
}
public List<string> GetClassMembers()
{
return _myClassMember;
}
public void PrintMembers()
{
Console.WriteLine("No of items in member {0}", _myClassMember.Count.ToString());
}
}
这就是它的名称:
class Program
{
static void Main(string[] args)
{
MyClass cls = new MyClass();
cls.PrintMembers();
IList<string> lst = cls.GetClassMembers();
lst.Add("Item1");
lst.Add("Item2");
cls.PrintMembers();
}
}
它打印如下:
No of items in member 0
No of items in member 2
能够在外面更新私人成员是正确的吗?