Why is internal protected not more restrictive than internal?

后端 未结 8 2153
陌清茗
陌清茗 2020-12-08 04:33

I\'d like to create an internal auto-property:

internal bool IP { get; protected internal set; }

I thought it would be possible to make the

8条回答
  •  心在旅途
    2020-12-08 05:13

    At the .NET level, there are two similar but distinct access levels:

    • FamilyAndAssembly: more restrictive than either protected or internal
    • FamilyOrAssembly: less restrictive than either protected or internal

    "protected internal" in C# means FamilyOrAssembly; there's no modifier for FamilyAndAssembly.

    So, your protected internal setter is less restrictive than the internal overall property. What you could do is:

    protected internal bool IP { internal get; set; }
    

    But then your setter is less restricted than your getter, which is odd...

    Another (somewhat equivalent) alternative is:

    internal bool IP { get; set; }
    
    protected void SetIP(bool ip)
    {
        this.IP = ip;
    }
    

提交回复
热议问题