views:

218

answers:

3

Ok, so this may be a bit of a silly question, and there's certainly the obvious answer, but I was curious if I've missed any subtleties here.

Is there any difference in terms of visibility/usability between a public member declared in an internal class and an internal member declared in an internal class?

i.e. between

internal class Foo
{
    public void Bar()
    {
    }
}

and

internal class Foo
{
    internal void Bar()
    {
    }
}

If you declared the method as public and also virtual, and then overrode it in a derived class that is public, the reason for using this modifier is clear. However, is this the only situation... am I missing something else?

A: 

public members of an internal class can override public members of public base classes and, therefore, be a little more exposed... if indirectly.

+5  A: 

A public member is still just internal when in an internal class.

From MSDN:

The accessibility of a member can never be greater than the accessibility of its containing type. For example, a public method declared in an internal type has only internal accessibility

Think of it this way, I would access a public property on....? A class I can't see? :)

Eric's answer is very important in this case, if it's exposed via an interface and not directly it does make a difference, just depends if you're in that situation with the member you're dealing with.

Nick Craver
Yes, this was indeed exactly my thought except for in the case Eric pointed out (as well as the one in my original question).
Noldorin
+12  A: 

Consider this case:

public interface IBar { void Bar(); }
internal class C : IBar
{
    public void Bar() { }
}

Here C.Bar cannot be marked as internal; doing so is an error because C.Bar can be accessed by a caller of D.GetBar():

public class D
{
    public static IBar GetBar() { return new C(); } 
}
Eric Lippert
Thanks Eric, this is another good case. So effectively interface implementation and class inheritance are the reasons for allowing either the `public` or `internal` modifier in an `internal` class. In other cases, they are equivalent it would seem.
Noldorin