访问修饰符(C# 编程指南)Access Modifiers (C# Programming Guide)

所有类型和类型成员都具有可访问性级别。All types and type members have an accessibility level. 该级别可以控制是否可以从你的程序集或其他程序集中的其他代码中使用它们。The accessibility level controls whether they can be used from other code in your assembly or other assemblies. 可以使用以下访问修饰符在进行声明时指定类型或成员的可访问性:Use the following access modifiers to specify the accessibility of a type or member when you declare it:

  • public:同一程序集中的任何其他代码或引用该程序集的其他程序集都可以访问该类型或成员。public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
  • private:只有同一 classstruct 中的代码可以访问该类型或成员。private: The type or member can be accessed only by code in the same class or struct.
  • protected:只有同一 class 或者从该 class 派生的 class 中的代码可以访问该类型或成员。protected: The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
  • internal:同一程序集中的任何代码都可以访问该类型或成员,但其他程序集中的代码不可以。internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
  • protected internal:该类型或成员可由对其进行声明的程序集或另一程序集中的派生 class 中的任何代码访问。protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.
  • private protected:只有在其声明程序集内,通过相同 class 中的代码或派生自该 class 的类型,才能访问类型或成员。private protected: The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.

下面的示例演示如何在类型和成员上指定访问修饰符:The following examples demonstrate how to specify access modifiers on a type and member:

public class Bicycle
{
    public void Pedal() { }
}

不是所有访问修饰符都可以在所有上下文中由所有类型或成员使用。Not all access modifiers are valid for all types or members in all contexts. 在某些情况下,类型成员的可访问性受到其包含类型的可访问性的限制。In some cases, the accessibility of a type member is constrained by the accessibility of its containing type.

类和结构可访问性Class and struct accessibility

直接在命名空间中声明的类和结构(即,没有嵌套在其他类或结构中的类和结构)可以为 publicinternalClasses and structs declared directly within a namespace (in other words, that aren't nested within other classes or structs) can be either public or internal. 如果未指定任何访问修饰符,则默认设置为 internalinternal is the default if no access modifier is specified.

结构成员(包括嵌套的类和结构)可以声明为 publicinternalprivateStruct members, including nested classes and structs, can be declared public, internal, or private. 类成员(包括嵌套的类和结构)可以声明为 publicprotected internalprotectedinternalprivate protectedprivateClass members, including nested classes and structs, can be public, protected internal, protected, internal, private protected, or private. 默认情况下,类成员和结构成员(包括嵌套的类和结构)的访问级别为 privateClass and struct members, including nested classes and structs, have private access by default. 不能从包含类型的外部访问私有嵌套类型。Private nested types aren't accessible from outside the containing type.

派生类不能具有高于其基类型的可访问性。Derived classes can't have greater accessibility than their base types. 不能声明派生自内部类 A 的公共类 BYou can't declare a public class B that derives from an internal class A. 如果允许这样,则它将具有使 A 公开的效果,因为可从派生类访问 A 的所有 protectedinternal 成员。If allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

可以通过使用 InternalsVisibleToAttribute 启用特定的其他程序集访问内部类型。You can enable specific other assemblies to access your internal types by using the 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. 结构成员无法声明为 protectedprotected internalprivate protected,因为结构不支持继承。Struct members can't be declared as protected, protected internal, or private protected because structs don't support inheritance.

通常情况下,成员的可访问性不大于包含该成员的类型的可访问性。Normally, the accessibility of a member isn't greater than the accessibility of the type that contains it. 但是,如果内部类的 public 成员实现了接口方法或替代了在公共基类中定义的虚拟方法,则可从该程序集的外部访问该成员。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 field, property, or event must be at least as accessible as the member itself. 同样,任何方法、索引器或委托的返回类型和参数类型必须至少与该成员本身具有相同的可访问性。Similarly, the return type and the parameter types of any method, indexer, or delegate must be at least as accessible as the member itself. 例如,除非 C 也是 public,否则不能具有返回类 Cpublic 方法 MFor example, you can't have a public method M that returns a class C unless C is also public. 同样,如果 A 声明为 private,则不能具有类型 Aprotected 属性。Likewise, you can't have a protected property of type A if A is declared as private.

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

终结器不能具有可访问性修饰符。Finalizers can't have accessibility modifiers.

若要设置 classstruct 成员的访问级别,请向成员声明添加适当的关键字,如以下示例中所示。To set the access level for a class or struct member, add the appropriate keyword to the member declaration, as shown in the following example.

// public class:
public class Tricycle
{
    // protected method:
    protected void Pedal() { }

    // private field:
    private int wheels = 3;

    // protected internal property:
    protected internal int Wheels
    {
        get { return wheels; }
    }
}

其他类型Other types

在命名空间内直接声明的接口可以声明为 publicinternal,就像类和结构一样,接口默认设置为 internal 访问级别。Interfaces declared directly within a namespace can be public or internal and, just like classes and structs, interfaces default to internal access. 接口成员默认为 public,因为接口的用途是启用其他类型以访问类或结构。Interface members are public by default because the purpose of an interface is to enable other types to access a class or struct. 接口成员声明可以包含任何访问修饰符。Interface member declarations may include any access modifier. 这最适用于静态方法,以提供类的所有实现器需要的常见实现。This is most useful for static methods to provide common implementations needed by all implementors of a class.

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

委托类似于类和结构。Delegates behave like classes and structs. 默认情况下,当在命名空间内直接声明它们时,它们具有 internal 访问级别,当将它们嵌套在命名空间内时,它们具有 private 访问级别。By default, they have internal access when declared directly within a namespace, and private access when nested.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also