简体   繁体   English

C# 中的默认访问修饰符是什么?

[英]What are the default access modifiers in C#?

What is the default access modifier for classes, methods, members, constructors, delegates and interfaces?类、方法、成员、构造函数、委托和接口的默认访问修饰符是什么?

The default access for everything in C# is "the most restricted access you could declare for that member" . C# 中所有内容的默认访问权限是“您可以为该成员声明的最受限制的访问权限”

So for example:例如:

namespace MyCompany
{
    class Outer
    {
        void Foo() {}
        class Inner {}
    }
}

is equivalent to相当于

namespace MyCompany
{
    internal class Outer
    {
        private void Foo() {}
        private class Inner {}
    }
}

The one sort of exception to this is making one part of a property (usually the setter) more restricted than the declared accessibility of the property itself:一种例外是使属性的一部分(通常是 setter)比属性本身声明的可访问性更受限制:

public string Name
{
    get { ... }
    private set { ... } // This isn't the default, have to do it explicitly
}

This is what the C# 3.0 specification has to say (section 3.5.1):这就是 C# 3.0 规范必须说的(第 3.5.1 节):

Depending on the context in which a member declaration takes place, only certain types of declared accessibility are permitted.根据成员声明发生的上下文,仅允许某些类型的声明可访问性。 Furthermore, when a member declaration does not include any access modifiers, the context in which the declaration takes place determines the default declared accessibility.此外,当成员声明不包含任何访问修饰符时,声明发生的上下文决定了默认声明的可访问性。

  • Namespaces implicitly have public declared accessibility.命名空间隐含地具有公开声明的可访问性。 No access modifiers are allowed on namespace declarations.命名空间声明中不允许使用访问修饰符。
  • Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility.在编译单元或命名空间中声明的类型可以具有公共或内部声明的可访问性,并且默认为内部声明的可访问性。
  • Class members can have any of the five kinds of declared accessibility and default to private declared accessibility.类成员可以具有五种声明的可访问性中的任何一种,并且默认为私有声明的可访问性。 (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.) (请注意,声明为类成员的类型可以具有五种声明的可访问性中的任何一种,而声明为命名空间成员的类型只能具有公共或内部声明的可访问性。)
  • Struct members can have public, internal, or private declared accessibility and default to private declared accessibility because structs are implicitly sealed.结构成员可以具有公共、内部或私有声明的可访问性,并且默认为私有声明的可访问性,因为结构是隐式密封的。 Struct members introduced in a struct (that is, not inherited by that struct) cannot have protected or protected internal declared accessibility.在结构中引入的结构成员(即不被该结构继承)不能具有受保护或受保护的内部声明的可访问性。 (Note that a type declared as a member of a struct can have public, internal, or private declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.) (请注意,声明为结构成员的类型可以具有公共、内部或私有声明的可访问性,而声明为命名空间成员的类型只能具有公共或内部声明的可访问性。)
  • Interface members implicitly have public declared accessibility.接口成员隐式地具有公开声明的可访问性。 No access modifiers are allowed on interface member declarations.接口成员声明中不允许使用访问修饰符。
  • Enumeration members implicitly have public declared accessibility.枚举成员隐含地具有公开声明的可访问性。 No access modifiers are allowed on enumeration member declarations.枚举成员声明中不允许使用访问修饰符。

(Note that nested types would come under the "class members" or "struct members" parts - and therefore default to private visibility.) (请注意,嵌套类型将属于“类成员”或“结构成员”部分 - 因此默认为私有可见性。)

top level class: internal method: private members (unless an interface or enum): private (including nested classes) members (of interface or enum): public constructor: private (note that if no constructor is explicitly defined, a public default constructor will be automatically defined) delegate: internal interface: internal explicitly implemented interface member: public!

Short answer: minimum possible access (cf Jon Skeet's answer).简短回答:尽可能少的访问(参见 Jon Skeet 的回答)。

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 | public, internal 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 | All¹ interface | public | All¹ class | private | All¹ struct | private | public, internal, private² delegate | private | All¹ constructor | private | All¹ enum member | public | none (always implicitly public) 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 ² 结构不能从结构或类继承(尽管它们可以,接口),因此 protected 不是有效的修饰符

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#.注意:CIL 还提供了受保护的和内部的(与现有的受保护的“或”内部相反),但据我所知,这目前不适用于 C#。


See:看:

http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx 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/ms173121.aspx
http://msdn.microsoft.com/en-us/library/cx03xt0t.aspx http://msdn.microsoft.com/en-us/library/cx03xt0t.aspx
(Man I love Microsoft URLs...) (我喜欢微软的 URL……)

Have a look at Access Modifiers (C# Programming Guide)查看访问修饰符(C# 编程指南)

Class and Struct Accessibility类和结构可访问性

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.如果未指定访问修饰符,则默认为 Internal。

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, private protected or private.类成员,包括嵌套类和结构,可以是公共的、受保护的内部的、受保护的、内部的、私有的、受保护的或私有的。 The access level for class members and struct members, including nested classes and structs, is private by default.默认情况下,类成员和结构成员(包括嵌套类和结构)的访问级别是私有的。 Private nested types are not accessible from outside the containing type.不能从包含类型外部访问私有嵌套类型。

Derived classes cannot have greater accessibility than their base types.派生类不能比它们的基类型具有更大的可访问性。 In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.换句话说,您不能拥有从内部类 A 派生的公共类 B。如果允许这样做,则会产生使 A 成为公共的效果,因为 A 的所有受保护或内部成员都可以从派生类访问。

You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute .您可以使用InternalsVisibleToAttribute启用特定的其他程序集来访问您的内部类型。 For more information, see Friend Assemblies.有关详细信息,请参阅友元程序集。

Class and Struct Member Accessibility类和结构成员可访问性

Class members (including nested classes and structs) can be declared with any of the six types of access.可以使用六种访问类型中的任何一种来声明类成员(包括嵌套类和结构)。 Struct members cannot be declared as protected because structs do not support inheritance.结构成员不能声明为受保护的,因为结构不支持继承。

Normally, the accessibility of a member is not greater than the accessibility of the type that contains it.通常,成员的可访问性不大于包含它的类型的可访问性。 However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.但是,如果成员实现接口方法或覆盖在公共基类中定义的虚拟方法,则可以从程序集外部访问内部类的公共成员。

The type of any member that is a field, property, or event must be at least as accessible as the member itself.作为字段、属性或事件的任何成员的类型必须至少与成员本身一样可访问。 Similarly, the return type and the parameter types of any member that is a method, indexer, or delegate must be at least as accessible as the member itself.同样,作为方法、索引器或委托的任何成员的返回类型和参数类型必须至少与成员本身一样可访问。 For example, you cannot have a public method M that returns a class C unless C is also public.例如,您不能有一个返回类 C 的公共方法 M,除非 C 也是公共的。 Likewise, you cannot have a protected property of type A if A is declared as private.同样,如果 A 被声明为私有,则不能拥有 A 类型的受保护属性。

User-defined operators must always be declared as public and static.用户定义的运算符必须始终声明为 public 和 static。 For more information, see Operator overloading.有关详细信息,请参阅运算符重载。

Finalizers cannot have accessibility modifiers.终结器不能有可访问性修饰符。

Other Types其他类型

Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access.直接在命名空间中声明的接口可以声明为公共的或内部的,就像类和结构一样,接口默认为内部访问。 Interface members are always public because the purpose of an interface is to enable other types to access a class or struct.接口成员始终是公共的,因为接口的目的是使其他类型能够访问类或结构。 No access modifiers can be applied to interface members.不能将访问修饰符应用于接口成员。

Enumeration members are always public, and no access modifiers can be applied.枚举成员始终是公共的,并且不能应用访问修饰符。

Delegates behave like classes and structs.委托的行为类似于类和结构。 By default, they have internal access when declared directly within a namespace, and private access when nested.默认情况下,它们在命名空间中直接声明时具有内部访问权限,而嵌套时则具有私有访问权限。

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.结构成员默认是私有的。

I would like to add some documentation link.我想添加一些文档链接。 Check out more detail here .在此处查看更多详细信息。

在此处输入图像描述

Simplest answer is the following.....最简单的答案如下......

All members in C# always take the LEAST accessible modifier possible by default.默认情况下,C# 中的所有成员始终采用 LEAST 可访问修饰符。

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类 Animal内部类Animal 相同

interface Animal same as public interface Animal接口Animal公共接口Animal相同

In the case of nested classes and interfaces (inside types), the defaults are:对于嵌套类和接口(内部类型),默认值为:

class Animal same as private class Animal类 Animal私有类Animal 相同

interface Animal same as private interface Animal接口Animal私有接口Animal相同

If you just assume the default on c# types is always the most private , then you do not need to use custom accessors until you need to change the default.如果您只是假设 c# 类型的默认值始终是最私有的,那么您不需要使用自定义访问器,直到您需要更改默认值。 They are secure out of the box!它们开箱即用是安全的!

Namespace level: internal命名空间级别: internal

Type level: private类型级别: private

内部是默认修饰符

internal is default access modifier for class. internal是 class 的默认访问修饰符。

public is default access modifier for class members. public是 class 成员的默认访问修饰符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM