Reputation: 882
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:
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
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!
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
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:
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.
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
Reputation: 7028
Keyword protected
belongs to inheritance and keyword internal
belongs to scope.
Upvotes: 0
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
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