为什么以及如何C#允许访问类本身之外的私有变量时,它同样含有类中? [英] Why and how does C# allow accessing private variables outside the class itself when it's within the same containing class?

查看:244
本文介绍了为什么以及如何C#允许访问类本身之外的私有变量时,它同样含有类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这个问题是有足够的描述,但为何以及如何这种行为存在?

I don't know if the question is descriptive enough but why and how does this behaviour exist?:

public class Layer
{
    public string Name { get; set; }

    private IEnumerable<Layer> children;
    public IEnumerable<Layer> Children
    {
        get { return this.children.Where ( c => c.Name != null ).Select ( c => c ); }
        set { this.children = value; }
    }

    public Layer ( )
    {
        this.children = new List<Layer> ( ); // Fine

        Layer layer = new Layer ( );
        layer.children = new List<Layer> ( ); // Isn't .children private from the outside?
    }
}

我可以访问 layer.Children 的任何地方,这很好,但我怎么能访问 layer.children ,因为它是私人?

I can access layer.Children anywhere, that's fine, but how can I access layer.children since it's private?

Layer layer = new Layer ( );
layer.children = new List<Layer> ( );

只有当在code是图层类中。是否有特殊的code以区别对待访问私有变量,如果它包含类内部完成,即使访问是从外面?

only works if the code is inside the Layer class. Is there special code to treat accessing private variables differently if it's done inside the containing class, even though the access is from the outside?

我知道使用的原因:

this.children = ...

含有类中,但创建新实例,并从外部对其进行修改,即使他们仍然是含类中,似乎并不像一个很好的做法。

inside the containing class, but creating new instances and modifying them from the outside, even if they are still within the containing class, doesn't seem like a good practice.

有什么原因让这个?

推荐答案

请参见C#语言规范的第3.5.1。相关的文字是这样的:

See section 3.5.1 of the C# language specification. The relevant text is this:

私有的,这被选中   包括在私人修饰符   成员声明。直观   私人的含义是访问限制   它所属的类型。

Private, which is selected by including a private modifier in the member declaration. The intuitive meaning of private is "access limited to the containing type".

请注意,该修改是相关的键入的,不是的的实例。

Note that the modifier is relevant to the type, not the instance.

然后再在第3.5.2一些规则进一步解释说:

And then further in section 3.5.2 some rules are further explained:

在直观地讲,当一个类型或   构件M被访问,以下   步骤进行计算,以确保   访问被允许:

In intuitive terms, when a type or member M is accessed, the following steps are evaluated to ensure that the access is permitted:

      
  • 首先,如果M是在一个类型中声明的(相对于编译单元   或命名空间),编译时错误   如果发生这种类型的不可访问。
  •   
  • 然后,如果M是公共的,则允许进行访问。
  •   
  • 否则,如果M是内部保护,则当访问允许   它发生在程序中,其中   M被声明,或者如果它发生在   从类中派生的类   声明M和发生   通过派生类类型   (§3.5.3)。
  •   
  • 否则,如果M是受保护的,则访问被许可,如果它发生   类,其中M是内   宣告,或者如果它发生在一   从类派生类中,   声明M,并通过发生   派生类类型(​​§3.5.3)。
  •   
  • 否则,如果M是内部的,则允许进行访问如果它发生   该方案中,M是内   声明。
  •   
  • ,否则,如果M是私有的,则允许进行访问,如果发生   内,其中M是所述类型   声明。
  •   
  • 否则,类型或成员不可访问,并且编译时错误   发生。
  •   
  • First, if M is declared within a type (as opposed to a compilation unit or a namespace), a compile-time error occurs if that type is not accessible.
  • Then, if M is public, the access is permitted.
  • Otherwise, if M is protected internal, the access is permitted if it occurs within the program in which M is declared, or if it occurs within a class derived from the class in which M is declared and takes place through the derived class type (§3.5.3).
  • Otherwise, if M is protected, the access is permitted if it occurs within the class in which M is declared, or if it occurs within a class derived from the class in which M is declared and takes place through the derived class type (§3.5.3).
  • Otherwise, if M is internal, the access is permitted if it occurs within the program in which M is declared.
  • Otherwise, if M is private, the access is permitted if it occurs within the type in which M is declared.
  • Otherwise, the type or member is inaccessible, and a compile-time error occurs.

这篇关于为什么以及如何C#允许访问类本身之外的私有变量时,它同样含有类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆