Public and internal members in the inner class?

Okay, so this might be a bit of a dumb question, and certainly the obvious answer, but I was curious if I missed any subtleties here.

Is there any difference in visibility / usability between the public member declared in the internal class and the internal member declared in the internal class?

i.e. between

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

and

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

If you declared a method as public , as well as virtual , and then redefined it in the derived class public , the reason for using this modifier is clear. However, this is the only situation ... did I miss something else?

+49
public access-modifiers c # class internal
Apr 01 '10 at 23:14
source share
5 answers

Consider this case:

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

Here C.Bar cannot be marked as internal; this is an error, because access to C.Bar can call the calling object D.GetBar ():

 public class D { public static IBar GetBar() { return new C(); } } 
+47
Apr 01 '10 at 23:23
source share
Element

A public remains only internal when in the class internal .

From MSDN :

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

Think about it this way, would I get access to the public property on ....? A class that I do not see? :)

Eric's answer is very important in this case, if it is displayed through the interface, and not directly, it really matters, it just depends on whether you are in this situation with the member you are dealing with.

+28
Apr 01 '10 at 23:18
source share

If it comes to reflection, it is important whether the member is public or not:

For example, you can even pass a nested private class to a WPF binding, and the binding will work with public properties in the same way as usual.

+1
Jun 26 '12 at 15:57
source share

Just ran into another example, where is the difference between the two when used from XAML to WPF.

XAML:

 <Button Tag="{x:Static vm:Foo+Bar.e1}" /> 

Code with internal enum successfully compiles:

 internal class Foo { internal enum Bar { e1, e2, } } 

But unexpectedly changing it to public leads to an error:

 internal class Foo { public enum Bar { e1, e2, } } 

In the last example, a compilation error occurs:

error MC3064: only public or internal classes can be used in markup. The type "Bar" is not public or internal.

Unfortunately, I can’t explain what happened to public in this case. I assume that "just because WPF works that way." Just change the modifier of the nested class to internal to get rid of the error.

+1
Oct 08 '15 at 2:59
source share

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

0
Apr 01 '10 at 23:16
source share



All Articles