分享
 
 
 

10.5 Member access

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

10.5Member access

Declarations of members allow control over member access. The accessibility

of a member is established by

the declared accessibility (?0.5.1) of the member combined with the

accessibility of the immediately

containing type, if any.

When access to a particular member is allowed, the member is said to be

accessible. Conversely, when

access to a particular member is disallowed, the member is said to be

inaccessible. Access to a member is

permitted when the textual location in which the access takes place is

included in the accessibility domain

(?0.5.2) of the member.

C# LANGUAGE SPECIFICATION

74

10.5.1 Declared accessibility

The declared accessibility of a member can be one of the following:

?Public, which is selected by including a public modifier in the member

declaration. The intuitive

meaning of public is .access not limited..

?Protected, which is selected by including a protected modifier in the

member declaration. The

intuitive meaning of protected is .access limited to the containing class

or types derived from the

containing class..

?Internal, which is selected by including an internal modifier in the

member declaration. The intuitive

meaning of internal is .access limited to this program..

?Protected internal, which is selected by including both a protected and

an internal modifier in the

member declaration. The intuitive meaning of protected internal is .access

limited to this program

or types derived from the containing class..

?Private, which is selected by including a private modifier in the member

declaration. The intuitive

meaning of private is .access limited to the containing type..

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: 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. end note])

?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: 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. end note])

?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.

10.5.2 Accessibility domains

The accessibility domain of a member consists of the (possibly disjoint)

sections of program text in which

access to the member is permitted. For purposes of defining the

accessibility domain of a member, a member

is said to be top-level if it is not declared within a type, and a member

is said to be nested if it is declared

within another type. Furthermore, the text of an assembly is defined as all

source text contained in all source

files of that assembly, and the source text of a type is defined as all

source text contained between the

opening and closing .{. and .}. tokens in the class-body, struct-body,

interface-body, or enum-body of the

type (including, possibly, types that are nested within the type).

The accessibility domain of a predefined type (such as object, int, or

double) is unlimited.

The accessibility domain of a top-level type T that is declared in a

program P is defined as follows:

Chapter 10 Basic concepts

75

?If the declared accessibility of T is public, the accessibility domain of

T is the program text of P and

any program that references P.

?If the declared accessibility of T is internal, the accessibility domain

of T is the program text of P.

[Note: From these definitions it follows that the accessibility domain of a

top-level type is always at least the

program text of the program in which that type is declared. end note]

The accessibility domain of a nested member M declared in a type T within a

program P, is defined as

follows (noting that M itself may possibly be a type):

?If the declared accessibility of M is public, the accessibility domain of

M is the accessibility domain

of T.

?If the declared accessibility of M is protected internal, let D be the

union of the program text of P

and the program text of any type derived from T, which is declared outside

P. The accessibility domain

of M is the intersection of the accessibility domain of T with D.

?If the declared accessibility of M is protected, let D be the union of

the program text of T and the

program text of any type derived from T. The accessibility domain of M is

the intersection of the

accessibility domain of T with D.

?If the declared accessibility of M is internal, the accessibility domain

of M is the intersection of the

accessibility domain of T with the program text of P.

?If the declared accessibility of M is private, the accessibility domain

of M is the program text of T.

[Note: From these definitions it follows that the accessibility domain of a

nested member is always at least

the program text of the type in which the member is declared. Furthermore,

it follows that the accessibility

domain of a member is never more inclusive than the accessibility domain of

the type in which the member

is declared. end note]

[Note: In intuitive terms, when a type or member M is accessed, the

following steps are evaluated to ensure

that the access is permitted:

?First, if M is declared within a type (as opposed to a compilation unit

or a namespace), a compile-time

error occurs if that type is not accessible.

?Then, if M is public, the access is permitted.

?Otherwise, if M is protected internal, the access is permitted if it

occurs within the program in

which M is declared, or if it occurs within a class derived from the class

in which M is declared and takes

place through the derived class type (?0.5.3).

?Otherwise, if M is protected, the access is permitted if it occurs within

the class in which M is declared,

or if it occurs within a class derived from the class in which M is

declared and takes place through the

derived class type (?0.5.3).

?Otherwise, if M is internal, the access is permitted if it occurs within

the program in which M is

declared.

?Otherwise, if M is private, the access is permitted if it occurs within

the type in which M is declared.

?Otherwise, the type or member is inaccessible, and a compile-time error

occurs.

end note]

[Example: In the example

public class A

{

public static int X;

internal static int Y;

private static int Z;

}

C# LANGUAGE SPECIFICATION

76

internal class B

{

public static int X;

internal static int Y;

private static int Z;

public class C

{

public static int X;

internal static int Y;

private static int Z;

}

private class D

{

public static int X;

internal static int Y;

private static int Z;

}

}

the classes and members have the following accessibility domains:

?The accessibility domain of A and A.X is unlimited.

?The accessibility domain of A.Y, B, B.X, B.Y, B.C, B.C.X, and B.C.Y is

the program text of the

containing program.

?The accessibility domain of A.Z is the program text of A.

?The accessibility domain of B.Z and B.D is the program text of B,

including the program text of B.C

and B.D.

?The accessibility domain of B.C.Z is the program text of B.C.

?The accessibility domain of B.D.X and B.D.Y is the program text of B,

including the program text of

B.C and B.D.

?The accessibility domain of B.D.Z is the program text of B.D.

As the example illustrates, the accessibility domain of a member is never

larger than that of a containing

type. For example, even though all X members have public declared

accessibility, all but A.X have

accessibility domains that are constrained by a containing type. end

example]

As described in ?0.4, all members of a base class, except for instance

constructors, destructors, and static

constructors are inherited by derived types. This includes even private

members of a base class. However,

the accessibility domain of a private member includes only the program text

of the type in which the

member is declared. [Example: In the example

class A

{

int x;

static void F(B b) {

b.x = 1; // Ok

}

}

class B: A

{

static void F(B b) {

b.x = 1; // Error, x not accessible

}

}

the B class inherits the private member x from the A class. Because the

member is private, it is only

accessible within the class-body of A. Thus, the access to b.x succeeds in

the A.F method, but fails in the

B.F method. end example]

Chapter 10 Basic concepts

77

10.5.3 Protected access for instance members

When a protected instance member is accessed outside the program text of

the class in which it is

declared, and when a protected internal instance member is accessed outside

the program text of the

program in which it is declared, the access is required to take place

through an instance of the derived class

type in which the access occurs. Let B be a base class that declares a

protected instance member M, and let D

be a class that derives from B. Within the class-body of D, access to M can

take one of the following forms:

?An unqualified type-name or primary-expression of the form M.

?A primary-expression of the form E.M, provided the type of E is D or a

class derived from D.

?A primary-expression of the form base.M.

In addition to these forms of access, a derived class can access a

protected instance constructor of a base

class in a constructor-initializer (?7.10.1).

[Example: In the example

public class A

{

protected int x;

static void F(A a, B b) {

a.x = 1; // Ok

b.x = 1; // Ok

}

}

public class B: A

{

static void F(A a, B b) {

a.x = 1; // Error, must access through instance of B

b.x = 1; // Ok

}

}

within A, it is possible to access x through instances of both A and B,

since in either case the access takes

place through an instance of A or a class derived from A. However, within

B, it is not possible to access x

through an instance of A, since A does not derive from B. end example]

10.5.4 Accessibility constraints

Several constructs in the C# language require a type to be at least as

accessible as a member or another type.

A type T is said to be at least as accessible as a member or type M if the

accessibility domain of T is a

superset of the accessibility domain of M. In other words, T is at least as

accessible as M if T is accessible in

all contexts in which M is accessible.

The following accessibility constraints exist:

?The direct base class of a class type must be at least as accessible as

the class type itself.

?The explicit base interfaces of an interface type must be at least as

accessible as the interface type itself.

?The return type and parameter types of a delegate type must be at least

as accessible as the delegate type

itself.

?The type of a constant must be at least as accessible as the constant

itself.

?The type of a field must be at least as accessible as the field itself.

?The return type and parameter types of a method must be at least as

accessible as the method itself.

?The type of a property must be at least as accessible as the property

itself.

?The type of an event must be at least as accessible as the event itself.

?The type and parameter types of an indexer must be at least as accessible

as the indexer itself.

C# LANGUAGE SPECIFICATION

78

?The return type and parameter types of an operator must be at least as

accessible as the operator itself.

?The parameter types of an instance constructor must be at least as

accessible as the instance constructor

itself.

[Example: In the example

class A {.}

public class B: A {.}

the B class results in a compile-time error because A is not at least as

accessible as B. end example]

[Example: Likewise, in the example

class A {.}

public class B

{

A F() {.}

internal A G() {.}

public A H() {.}

}

the H method in B results in a compile-time error because the return type A

is not at least as accessible as the

method. end example]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有