C#访问修饰符金沙官网线上

 

类成员和结构成员的可访问性

 

 

不是所有访问修饰符都可以在所有上下文中由所有类型或成员使用,在某些情况下类型成员的可访问性受到其包含类型的可访问性的限制。 以下各节提供了有关可访问性的更多详细信息。

 1 // public class:
 2 public class Tricycle
 3 {
 4     // protected method:
 5     protected void Pedal() { }
 6 
 7     // private field:
 8     private int wheels = 3;
 9 
10     // protected internal property:
11     protected internal int Wheels
12     {
13         get { return wheels; }
14     }
15 }

受保护内部可访问性级别的意思是受保护“或”内部,而不是受保护“和”内部。  id="mt40" class="sentence" data-guid="6ea9974fdbeba121006bce33255cd864" data-source="In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes.">换句话说,可以从同一程序集内的任何类(包括派生类)中访问受保护的内部成员。  id="mt41" class="sentence" data-guid="69cde929a9305311cc990567ced1f593" data-source="To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.">若要限制为只有同一程序集内的派生类可以访问,请将类本身声明为内部,并将其成员声明为受保护。

  • protected internal
  • data-guid="b90d7af1e669e69e2f2ce43e0f0e6c22" data-source="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.">直接在命名空间中声明的类和结构(即,没有嵌套在其他类或结构中的类和结构)可以是公共类和结构,也可以是内部类和结构。  id="mt15" class="sentence" data-guid="44981978087c5fef1c5065af6361f93e" data-source="Internal is the default if no access modifier is specified.">如果不指定访问修饰符,则默认为 internal。
  • data-guid="b90d7af1e669e69e2f2ce43e0f0e6c22" data-source="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."> id="mt15" class="sentence" data-guid="44981978087c5fef1c5065af6361f93e" data-source="Internal is the default if no access modifier is specified."> id="mt16" class="sentence" data-guid="8d25c439c8339dfd24bfdbcc999cbeee" data-source="Struct members, including nested classes and structs, can be declared as public, internal, or private.">结构成员,包括嵌套的类和结构,可以声明为公共的、 内部的,或私人的。  data-guid="8223b2327210defeb617befd282fb8c5" data-source="Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private.">类成员(包括嵌套的类和结构)可以为公共的、受保护的内部、受保护的、内部的或私有的。  id="mt18" class="sentence" data-guid="0a5e6d7b9e914ecbb45159954931cedc" data-source="The access level for class members and struct members, including nested classes and structs, is private by default.">类成员和结构成员的访问级别,包括嵌套类和结构,默认为私有。  id="mt19" class="sentence" data-guid="fc93d7e8868bfd1d19b3c79bec7a8139" data-source="Private nested types are not accessible from outside the containing type.">不可以从包含类型之外访问私有嵌套类型。
  • data-guid="8d25c439c8339dfd24bfdbcc999cbeee" data-source="Struct members, including nested classes and structs, can be declared as public, internal, or private."> id="mt17" class="sentence" data-guid="8223b2327210defeb617befd282fb8c5" data-source="Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private."> id="mt18" class="sentence" data-guid="0a5e6d7b9e914ecbb45159954931cedc" data-source="The access level for class members and struct members, including nested classes and structs, is private by default."> id="mt19" class="sentence" data-guid="fc93d7e8868bfd1d19b3c79bec7a8139" data-source="Private nested types are not accessible from outside the containing type."> id="mt20" class="sentence" data-guid="043516b010df2e211345b63e798fd092" data-source="Derived classes cannot have greater accessibility than their base types.">派生类的可访问性不能高于其基类型。  id="mt21" class="sentence" data-guid="986aaf405f5fa458077e733b9d10cd40" data-source="In other words, you cannot have a public class <span class="code">B that derives from an internal class <span class="code">A.">换句话说,不能有从内部类  class="code">A 派生的公共类 B。  class="sentence" data-guid="d24e98f0e71497a829130c107d87f265" data-source="If this were allowed, it would have the effect of making <span class="code">A public, because all protected or internal members of <span class="code">A are accessible from the derived class.">如果允许这种情况,将会使  class="code">A 成为公共类,因为  class="code">A 的所有受保护的成员或内部成员都可以从派生类访问。
  • data-guid="043516b010df2e211345b63e798fd092" data-source="Derived classes cannot have greater accessibility than their base types."> id="mt21" class="sentence" data-guid="986aaf405f5fa458077e733b9d10cd40" data-source="In other words, you cannot have a public class <span class="code">B that derives from an internal class <span class="code">A."> id="mt23" class="sentence" data-guid="85b1855c8c50b65204b112eb2d11dbbb" data-source="You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute.">可以使用 InternalsVisibleToAttribute 使其他某些程序集能够访问您的内部类型。
  • private

data-guid="b2ffcf8006e2cdec006715fdee23c42a" data-source="The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.">由其声明的程序集或另一个程序集派生的类中任何代码都可访问的类型或成员。  id="mt8" class="sentence" data-guid="48e9212800cd45a146b011a4597f1d43" data-source="Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.">从另一个程序集进行访问必须在类声明中发生,该类声明派生自其中声明受保护的内部元素的类,并且必须通过派生的类类型的实例发生。

C#中所有类型和类型成员都具有可访问性级别,用来控制是否可以在您程序集的其他代码中或其他程序集中使用它们。可使用以下访问修饰符指定声明类型或成员时类型或成员的可访问性。

data-guid="47fd6fd3789925a7a31df71b14c3caf9" data-source="The type or member can be accessed by any code in the same assembly, but not from another assembly.">同一程序集中的任何代码都可以访问该类型或成员,但其他程序集中的代码不可以。

  • data-guid="13a5244c98d496490ae5bf068ad1a005" data-source="Class members (including nested classes and structs) can be declared with any of the five types of access.">可以使用五种访问类型中的任何一种来声明类成员(包括嵌套的类和结构)。  id="mt27" class="sentence" data-guid="3585fa197e37e4bd861dee5526567eee" data-source="Struct members cannot be declared as protected because structs do not support inheritance.">结构成员无法声明为受保护成员,因为结构不支持继承。
  • data-guid="eed2b7e7dbaf83f28365719f5d12fdb6" data-source="Normally, the accessibility of a member is not greater than the accessibility of the type that contains it.">通常,成员的可访问性低于包含成员的类型的可访问性。  id="mt29" class="sentence" data-guid="8adfdf4bb9072ae4ddf8ead780116593" data-source="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.">如果由成员实现接口方法或重写已在公共基类中定义的虚拟方法时,内部类的公共成员可以从外部程序集访问。
  • data-guid="a62cea9e626d8aa47bd362899ddbcb15" data-source="The type of any member that is a field, property, or event must be at least as accessible as the member itself.">任何成员的字段、 属性或事件的类型必须至少与该成员本身一样具备可访问性。  id="mt31" class="sentence" data-guid="1e463ce6c41dc88e3d0777b8f1a6a0ff" data-source="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.">同样,作为方法、索引器或代表的任一成员的返回类型和参数类型必须至少有与该成员本身一样的可访问性。  id="mt32" class="sentence" data-guid="10264ee48d701636f0e59ef857c93218" data-source="For example, you cannot have a public method <span class="code">M that returns a class <span class="code">C unless <span class="code">C is also public.">例如,如果  class="code">C 不是公共类,则不能返回类  class="code">C 的公共方法 M。  class="sentence" data-guid="20fd50dbc3b1df84f7dbc41af1b2ff1e" data-source="Likewise, you cannot have a protected property of type <span class="code">A if <span class="code">A is declared as private.">同样,如果  class="code">A 声明为私有,则类型  class="code">A 不能有受保护的属性。
  • data-guid="bf443dc0f4d894c9d808c0af5c113009" data-source="User-defined operators must always be declared as public.">用户定义的运算符必须始终声明为公共运算符。 
  • data-guid="2eecbd02a54ce0262b79f40dbd24de70" data-source="Destructors cannot have accessibility modifiers.">析构函数不能具有可访问性修饰符。

data-guid="49f231c6f130a80ca5555bfdfb9b5d6e" data-source="The type or member can be accessed by any other code in the same assembly or another assembly that references it.">同一程序集中的任何其他代码或引用该程序集的其他程序集都可以访问该类型或成员。

  • protected

要设置类成员或结构成员的访问级别,请向该成员声明添加适当的关键字,如下面的示例所示。

  • data-guid="02d228ab23eb93d3cac277dba0ef61ee" data-source="Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access.">直接用命名空间声明时,可以将接口声明为公共接口或内部接口,只与类和结构一样,接口默认具有内部可访问性。  id="mt44" class="sentence" data-guid="0c41e1dcd05a4a4b9786fcefab469dd0" data-source="Interface members are always public because the purpose of an interface is to enable other types to access a class or struct.">接口成员始终是公共成员,因为接口的用途是让其他类型能够访问某个类或结构。  id="mt45" class="sentence" data-guid="4472d215675ebc3fae553b5bf7bdbe5d" data-source="No access modifiers can be applied to interface members.">访问修饰符不能应用于接口成员。
  • data-guid="02d228ab23eb93d3cac277dba0ef61ee" data-source="Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access."> id="mt44" class="sentence" data-guid="0c41e1dcd05a4a4b9786fcefab469dd0" data-source="Interface members are always public because the purpose of an interface is to enable other types to access a class or struct."> id="mt45" class="sentence" data-guid="4472d215675ebc3fae553b5bf7bdbe5d" data-source="No access modifiers can be applied to interface members."> id="mt46" class="sentence" data-guid="ba785dc40cd2e81edc5814c6875adead" data-source="Enumeration members are always public, and no access modifiers can be applied.">枚举成员始终是公共的,不能应用任何访问修饰符。
  • data-guid="ba785dc40cd2e81edc5814c6875adead" data-source="Enumeration members are always public, and no access modifiers can be applied."> id="mt47" class="sentence" data-guid="c96378eb830b09cd7e5db066468e410f" data-source="Delegates behave like classes and structs.">委托行为类似于类和结构。  id="mt48" class="sentence" data-guid="5816f5a6e035f01cccccd81ec58a3bf2" data-source="By default, they have internal access when declared directly within a namespace, and private access when nested.">默认情况下,它们在命名空间中直接声明时具有内部访问权,在嵌套时具有私有访问权。
  • public

data-guid="68043d981679e7830af4b74913e38d9c" data-source="The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.">只有同一类或结构或者此类的派生类中的代码才可以访问的类型或成员。

下面的示例演示如何为类型和成员指定访问修饰符: 

说明

其他类型

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

 

data-guid="d0059048b04c969d73b5982b2ac39549" data-source="The type or member can be accessed only by code in the same class or struct.">只有同一类或结构中的代码可以访问该类型或成员。

  • 金沙官网线上,internal

类和结构的可访问性

本文由金沙官网线上发布于编程,转载请注明出处:C#访问修饰符金沙官网线上

您可能还会对下面的文章感兴趣: