When I declare a class as internal, why does the IL show it as private?

前端 未结 3 894
[愿得一人]
[愿得一人] 2021-01-13 10:32

If I declare a class as internal, why does the IL show it as private?

internal class Thing

.class private auto ansi beforefieldinit Thing.Thing
       exten         


        
3条回答
  •  余生分开走
    2021-01-13 10:58

    The mapping of C# keywords to IL keywords isn't always a logical one. Ecma-335, section II.23.1.15 shows what flags are valid for a type. You'll see that it only defines Public, NotPublic and a set of NestedXxx flags. Nothing similar to "internal". So your class is actually NotPublic, displayed as "private" in ildasm.

    It is easy to see a side-effect of this: try this declaration in your C# code:

    private class DoesNotWork {
    }
    

    You'll get:

    error CS1527: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

提交回复
热议问题