tags:

views:

175

answers:

4

Can anyone explain, why we can do such thing and why we need this

public class OuterClass
{
  public class InnerClass 
  {
  }
}

Why we need public inner whatever: struct, class, enum or static class?

I think that if it is inner then it must be only private or protected.

+9  A: 

You don't generally need public nested types - but they can be useful sometimes. They make it very clear that one type is explicitly associated with another.

They also allow the nested type to access the private members of the enclosing type (in C#), which may be useful at times. I would guess that List<T>.Enumerator may do that, for example. In Java the access rules work the other way round - the enclosing class has access to the private members of the nested classes.

I think would have to explain why you'd want to explicitly prohibit this rather than why it's needed, as such. I can't think of anywhere else that you can specify an access modifier but can't make it public, other than for inconsistency (declaring a public method with an internal return type, for example).

Jon Skeet
You rock, Jon! Really, awesome explanation
Peter Perháč
Thank you for the answer, now it is clear to me. The reason I asked this question, cause I saw how our junior developer made enum nested and than used it as parameter for the class's method. So it looks odd to me, cause I had never seen such code earlier.OuterClass obj = new OuterClass();obj.Method(OuterClass.InnerEnum.Value1);
anderhil
"Looking odd" isn't a reason for something to be disallowed ;-)
Dan Puzey
Yeah, I know :) I couldn't think of any case it is needed for, so i tried to find some explanation why it is needed at all and didn't found one, so I asked this question here, and found an answer ! :)But still i think, that in the case I've shown it is odd :) I used to write in the manner that Microsoft does in .net framework (I'm c# developer), and I don't remember where they use enums in such way. Even if it is used only in one class.
anderhil
@anderhil: I could swear I've seen an enum like that *somewhere* in .NET, but I can't think of one offhand.
Jon Skeet
Today I have accidentally found where .NET uses such enum :) I was looking for how to get program files directory through Environment class and found such methodEnvironment.GetFolderPath where it uses SpecialFolder enum(which is in Environment class :) ) as parameter, so my code looks like thisEnvironment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
anderhil
+2  A: 

Consider this example:

public class TrainingFile {

    public List<TrainingFileEntry> getEntries() {
           return ...
    }

    public class TrainingFileEntry {
        int x;
        int y;
   }
}

You want to hide the details of the TrainingFileEntry, but your customer needs to be able to use it. Ofc you could create an own file for this nested class, but such a nested helper class ist mostly not a "standalone" and stuck to the class where it is nested. -)

InsertNickHere
+1 on that - i use something like this for exampple a lot with WPF controls, who may have inner classes as view parts (list entries etc.).... keeps the actual namespace cleaner.
TomTom
A: 

Its very useful when every member of InnerClass really does belong to one, and only one member of outer class. I have used that construct where OuterClass was a collection of mathematical objects and InnerClass was the actual objects themselves.

John Robertson
+2  A: 

Public inner types are generally not recommended, see this reference http://msdn.microsoft.com/en-us/library/tdz1bea9(v=VS.71).aspx

...but since there are (may be) exceptions, this is still allowed

Mahol25