Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protected internal class inside a namespace compiles without error

protected internal class foo
{
    //this compiles without any errors
}

also

internal class bar
{
    public int quix;
    protected internal int zyx;
    //this compiles without any errors
}

Are these compiler bugs or my misinterpretation of the standard?

Explanation:

  1. Classes can't have protected internal access modifier, only public or internal according to MSDN (Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified).
  2. Not all access modifiers can be used by all types or members in all contexts, and in some cases the accessibility of a type member is constrained by the accessibility of its containing type (MSDN). Public should fail. Protected internal is ambiguous for me - internal modifier is not necessary.

Edit: The fact that I'm using Mono is unnecessary cause the question was about what standard says and not what MONO does or does not. Maybe I'm coding my own compiler. That's why I quoted MSDN to be precise what is allowed and what is not.

like image 916
user206334 Avatar asked Jan 23 '13 09:01

user206334


People also ask

What is the correct definition of protected internal?

protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly. private protected: The type or member can be accessed by types derived from the class that are declared within its containing assembly.

What is private protected in C#?

The private protected keyword combination is a member access modifier. A private protected member is accessible by types derived from the containing class, but only within its containing assembly. For a comparison of private protected with the other access modifiers, see Accessibility Levels.

What is difference between internal and public in C#?

public is visible from wherever. internal is visible only within an assembly. You can then put a lot of sophistication on a method, but "protect" it using facade methods that may help the programmer to call the method correctly.

Which of the following is the default access modifier in a namespace?

Default access modifiers at Namespace level are internal. Default access modifiers at Class level are private.


2 Answers

As mentioned in my comment above, protected internal means protected or internal NOT protected and internal. No bug here :)

Further information/explanation is on haacked

In response to your questions:

  1. A class within a namespace (and not within another class) can only be declared as public or internal. HOWEVER, a class within another class can be declared as protected internal, private, etc.

  2. Yes, protected internal can be used inside a class whose access modifier is more strict than it's members, see example of a perfectly valid usage below (note that the class is inside the Program class):

    public class Program
    {
        static void Main(string[] args)
        {
        }
    
        private class Foo
        {
            private int priv { get; set; }
            protected internal int protint { get; set; }
            public int pub { get; set; }
        }
    }
    
like image 131
mattytommo Avatar answered Sep 19 '22 16:09

mattytommo


From Access Modifiers (C# Programming Guide)

protected internal

The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.

like image 22
Soner Gönül Avatar answered Sep 17 '22 16:09

Soner Gönül