Nested classes within .NET Framework

  • Thread starter Thread starter Christopher Ireland
  • Start date Start date
C

Christopher Ireland

Hi --

I'm trying to find an example of a nested class implemented within the .NET
Framework itself but without much success. I don't suppose that anybody
knows of such an example off the top of their head, do they?

Many thanks!
 
If you are thinking of classes within classes (like java), I believe .Net
uses structs instead. Those can contain methods and implement interfaces
(not sure about inheritance).
 
Christopher Ireland said:
I'm trying to find an example of a nested class implemented within the .NET
Framework itself but without much success. I don't suppose that anybody
knows of such an example off the top of their head, do they?

Well, the following shows all the *public* nested types within
mscorlib.dll:

using System;
using System.Reflection;

class Test
{
static void Main()
{
Assembly mscorlib = typeof(string).Assembly;

Type[] types = mscorlib.GetTypes();

foreach (Type t in types)
{
if (t.IsNestedPublic)
{
Console.WriteLine(t);
}
}
}
}

This only yields
System.Environment+SpecialFolder
System.Runtime.InteropServices.ELEMDESC+DESCUNION
System.Runtime.InteropServices.VARDESC+DESCUNION

There may be others in System.dll etc. In particular, there are loads
in System.Windows.Forms.dll - although many of those don't have public
enclosing type (which makes the nested type being public pretty
pointless, I think).

To see *all* nested types in an assembly (including automatically
generated ones) change the test to

if (t.DeclaringType != null)
 
Morten Wennevik said:
If you are thinking of classes within classes (like java), I believe .Net
uses structs instead. Those can contain methods and implement interfaces
(not sure about inheritance).

No, structs are entirely different from nested classes. It's true that
..NET doesn't have any equivalent to Java's inner classes. Even nested
types in .NET are slightly different to Java's static nested classes in
terms of access between the two classes.
 
Back
Top