user206334
user206334

Reputation: 882

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.

Upvotes: 5

Views: 5950

Answers (5)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64923

In addition, I really doubt that the "protected and internal class" would ever compile if the class was declared as a member of some namespace:

C# compiler said:

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

Protected classes will be always nested classes!

UPDATE

Since you're trying your code sample in some version of Mono compiler, and you said in your sample code in your question //this compiles without any errors, I couldn't understand why you didn't tagged the question for Mono.

The standard is the Microsoft C# compiler behavior. If you ask a question about "why something compiles" and you don't mention that you're not using the official one, you're just making the assumption that any compiler would compile your code.

Do you want to know which is the standard? It's -again-: Protected classes will be always nested classes!

Upvotes: 2

Mathew Thompson
Mathew Thompson

Reputation: 56429

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; }
        }
    }
    

Upvotes: 22

D J
D J

Reputation: 7028

Keyword protected belongs to inheritance and keyword internal belongs to scope.

Upvotes: 0

Mario
Mario

Reputation: 36487

To quote the MSDN entry on this:

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. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

So the declaration makes perfect sense, it's just working differently as expected when used outside a class.

Upvotes: 2

Soner Gönül
Soner Gönül

Reputation: 98750

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.

Upvotes: 2

Related Questions