Question

The default Access modifier of a class in C# is internal. But on inspecting the class with ildasm it shows class as private.

.class private auto ansi beforefieldinit ConsoleApplication1.Program
    extends [mscorlib]System.Object
    {
    } // end of class ConsoleApplication1.Program

Any idea why?

Was it helpful?

Solution

IL does not have the concept of internal, internal classes are represented as private if they are in the root namespace or assembly if they are nested inside another type.

namespace ConsoleApplication1
{
    internal class ExplicitInternal
    {
        private class ExplicitPrivate
        {
        }

        internal class ExplicitNestedInternal
        {
        }

        public class ExplicitNestedPublic
        {
        }
    }

    public class ExplicitPublic
    {
    }

    class ImplicitInternal
    {
        private class ImplicitPrivate
        {
        }
    }
}

becomes

.namespace ConsoleApplication1
{
    .class private auto ansi beforefieldinit ConsoleApplication1.ExplicitInternal
        extends [mscorlib]System.Object
    {
        .class nested private auto ansi beforefieldinit ExplicitPrivate
            extends [mscorlib]System.Object
        {
        }

        .class nested assembly auto ansi beforefieldinit ExplicitNestedInternal
            extends [mscorlib]System.Object
        {
        }

        .class nested public auto ansi beforefieldinit ExplicitNestedPublic
            extends [mscorlib]System.Object
        {
        }
    }

    .class public auto ansi beforefieldinit ConsoleApplication1.ExplicitPublic
        extends [mscorlib]System.Object
    {
    }

    .class private auto ansi beforefieldinit ConsoleApplication1.ImplicitInternal
        extends [mscorlib]System.Object
    {
        .class nested private auto ansi beforefieldinit ImplicitPrivate
            extends [mscorlib]System.Object
        {
        }
    }
}

OTHER TIPS

The general rule for the default access modifier is going to be the least accessibility possible. MSDN goes into more detail:

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default.

...

The type of any member that is a field, property, or event must be at least as accessible as the member itself.

So in C# these are the rules, however I agree with comments that the particular issue you are dealing with is ultimately due to internal having no meaning in IL.

Is it an inner class? Would be private then.

Class cannot be created as private by default because for soin that class must be at least nested inside another class. ILDASM show you that class is private because it's mean that class limited in scope to the type in which its declared. If a class is not declared inside the definition of a type then it cannot be private to that type. So, nested classes are private by default while non-nested classes are internal by default. If you dont add some access modifies it's automatically adopt to access level. means

class Myclass1 {
     class MyNestedClass
     {
     }
}

i the same like

internal class Myclass1 {
     private class MyNestedClass
     {
     }
}

Additional information MSDN1 and MSDN2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top