c# - 为什么下面类的定义可以访问同一个类的另一个对象的私有(private)数据成员?

标签 c#

为什么下面类的定义可以访问another的private数据成员other._nameother._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/

相关文章:

c# - C#打开一个新的WinForm并关闭创建它的Form

c# - 拆分字符串时获取额外的空白项

c# - Polly RetryForever 没有重试

c# - 如何在没有 async/await 的情况下循环异步方法

c# - string.Format 在 if 语句内时在 C# razor 中不起作用

javascript - 隐藏字段在代码隐藏中失去值(value)?

c# - 在 asp.net 中的标签之间添加超链接

c# - 在套接字 C# 中发送 ACK

c# - 分析用户资格数据的最佳方式(C# 和 SQL 服务器)

c# - Visual Studio 2017 : . rptproj 与 .sqlproj