Search code examples
c#ildasm

Default Access modifier of a class in C#


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?


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
            {
            }
        }
    }