What are the default access modifiers in C#?

后端 未结 9 1377
暖寄归人
暖寄归人 2020-11-22 03:44

What is the default access modifier for classes, methods, members, constructors, delegates and interfaces?

9条回答
  •  别跟我提以往
    2020-11-22 04:04

    Simplest answer is the following.....

    All members in C# always take the LEAST accessible modifier possible by default.

    That is why all top level classes in an assembly are "internal" by default, which means they are public to the assembly they are in, but private or excluded from access to outside assemblies. The only other option for a top level class is public which is more accessible. For nested types its all private except for a few rare exceptions like members of enums and interfaces which can only be public. Some examples. In the case of top level classes and interfaces, the defaults are:

    class Animal same as internal class Animal

    interface Animal same as public interface Animal

    In the case of nested classes and interfaces (inside types), the defaults are:

    class Animal same as private class Animal

    interface Animal same as private interface Animal

    If you just assume the default is always the most private, then you do not need to use an accessors until you need to change the default. Easy.

提交回复
热议问题