What are the default access modifiers in C#?

2018-12-31 08:35发布

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

9条回答
时光乱了年华
2楼-- · 2018-12-31 09: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.

查看更多
唯独是你
3楼-- · 2018-12-31 09:05

Internal is the default modifier

查看更多
听够珍惜
4楼-- · 2018-12-31 09:10

Namespace level: internal

Type level: private

查看更多
若你有天会懂
5楼-- · 2018-12-31 09:16

I would like to add some documentation link. Check out more detail here.

enter image description here

查看更多
弹指情弦暗扣
6楼-- · 2018-12-31 09:18

Short answer: minimum possible access (cf Jon Skeet's answer).

Long answer:

Non-nested types, enumeration and delegate accessibilities (may only have internal or public accessibility)

                     | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | none (always implicitly public)

interface            | internal  | public, internal

class                | internal  | public, internal

struct               | internal  | public, internal

delegate             | internal  | public, internal

Nested type and member accessiblities

                     | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | none (always implicitly public)

interface            | public    | none

class                | private   | All¹

struct               | private   | public, internal, private²

delegate             | private   | All¹

constructor          | private   | All¹

interface member     | public    | none (always implicitly public)

method               | private   | All¹

field                | private   | All¹

user-defined operator| none      | public (must be declared public)

¹ All === public, protected, internal, private, protected internal

² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier

The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.

Note: CIL also has the provision for protected and internal (as opposed to the existing protected "or" internal), but to my knowledge this is not currently available for use in C#.


See:

http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx
http://msdn.microsoft.com/en-us/library/ms173121.aspx
http://msdn.microsoft.com/en-us/library/cx03xt0t.aspx
(Man I love Microsoft URIs...)

查看更多
梦醉为红颜
7楼-- · 2018-12-31 09:19

Class is Internal by default.

  • Class members are private by default.

Interface is Internal by default.

  • Interface members are public by default. (Interfaces won't allow us to specify any kind of accessibility to it's members.)

    Note: If you try to specify any access specifier to interface's members then, it shows compile error.

Struct is Internal by default.

  • Struct members are private by default.
查看更多
登录 后发表回答