为什么下面类的定义可以访问another的private数据成员other._name
和other._id
同一类的对象?
谢谢。
public class MyData : IEquatable<MyData>
{
private long _id;
private string _name;
public bool Equals(MyData other)
{
bool ret =
string.Equals(_name, other._name) &&
long.Equals(_id, other._id);
return ret;
}
}
最佳答案
如果您查看 private
的文档你看到这个:
The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared, as in this example:
就像上面的示例一样,您正在访问该类中的那些私有(private)成员。它是另一个实例并不重要,您仍然在类的主体中,因此可以访问所有内容。
关于c# - 为什么下面类的定义可以访问同一个类的另一个对象的私有(private)数据成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42017399/