Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are nested types implicitly static?

In the C# 5.0 language specification it says:

Note that constants and nested types are classified as static members.

so if I write:

class A
{
   int a;

   class B
   {
       public void foo()
       {
           int j = a; // ERROR
       } 
   }
}

the assignment of a to j give me a CS0120 error

"An object reference is required for the nonstatic field, method, or property 'member'"

so I could understand that also foo is implicit static.

However when I look both the decompiled code and IL code there is no indication of static keyword!

internal class A
{
    private class B
    {
        public void foo()
        {
        }
    }

    private int a;
}


// Nested Types
    .class nested private auto ansi beforefieldinit B
        extends [mscorlib]System.Object
    {
        // Methods
        .method public hidebysig 
            instance void foo () cil managed 
        {

        } 
        ...
    }

Is a nested type really a static type with all methods implicitly static?

like image 779
xdevel2000 Avatar asked Dec 05 '22 19:12

xdevel2000


1 Answers

"Nested types are classified as static members"

Is a nested type really a static type with all methods implicitly static?

No - the definition of the Type is a static member, but the type itself is not static.

When it says

Note that constants and nested types are classified as static members.

it means you can create an instance of a nested class without an instance of the parent class.

In other words, with the code

public class Parent
{
    public class Child
    {
    }
}

to call new Parent.Child(), you don't need to first call new Parent() or something similar.


The problem with your code

Your actual problem is that an instance of the nested type is separate from instances of the parent type - your code,

class A
{
   int a;

   class B
   {
       public void foo()
       {
           int j = a; // ERROR
       } 
   }
}

does not compile because in A.B.foo(), the field a is an instance (i.e. not static) member of A, and B has no instance of A to refer to.


How to use One way of using nested types

You can think of type nesting as a way of extending accessibility of private members in the parent to the child, e.g.

public class Parent
{
    private int field;

    public class Child
    {
        public void WriteFieldFromParent(Parent parent)
        {
            Console.WriteLine(parent.field);
        }
    }
}

which compiles! You can get the value of the private field field in Parent from the Parent.Child class because it's nested. Note that I need to pass an instance of Parent to Parent.Child.WriteFieldFromParent.

In fact, the MSDN page on nested types (I don't know why I didn't refer to it when first writing this answer) gives an example similar to mine, and says:

A nested type has access to all of the members that are accessible to its containing type. It can access private and protected members of the containing type, including any inherited protected members.

like image 191
Wai Ha Lee Avatar answered Dec 08 '22 09:12

Wai Ha Lee