分享
 
 
 

10.7 Scopes

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

10.7 Scopes

The scope of a name is the region of program text within which it is

possible to refer to the entity declared

by the name without qualification of the name. Scopes can be nested, and an

inner scope may redeclare the

meaning of a name from an outer scope. [Note: This does not, however,

remove the restriction imposed by

?0.3 that within a nested block it is not possible to declare a local

variable with the same name as a local

variable in an enclosing block. end note] The name from the outer scope is

then said to be hidden in the

region of program text covered by the inner scope, and access to the outer

name is only possible by

qualifying the name.

?The scope of a namespace member declared by a

namespace-member-declaration (?6.4) with no

enclosing namespace-declaration is the entire program text.

?The scope of a namespace member declared by a

namespace-member-declaration within a namespacedeclaration

whose fully qualified name is N, is the namespace-body of every

namespace-declaration

whose fully qualified name is N or starts with N, followed by a period.

?The scope of a name defined or imported by a using-directive (?6.3)

extends over the namespacemember-

declarations of the compilation-unit or namespace-body in which the

using-directive occurs. A

using-directive may make zero or more namespace or type names available

within a particular

compilation-unit or namespace-body, but does not contribute any new members

to the underlying

declaration space. In other words, a using-directive is not transitive,

but, rather, affects only the

compilation-unit or namespace-body in which it occurs.

?The scope of a member declared by a class-member-declaration (?7.2) is

the class-body in which the

declaration occurs. In addition, the scope of a class member extends to the

class-body of those derived

classes that are included in the accessibility domain (?0.5.2) of the

member.

C# LANGUAGE SPECIFICATION

80

?The scope of a member declared by a struct-member-declaration (?8.2) is

the struct-body in which the

declaration occurs.

?The scope of a member declared by an enum-member-declaration (?1.3) is

the enum-body in which the

declaration occurs.

?The scope of a parameter declared in a method-declaration (?7.5) is the

method-body of that methoddeclaration.

?The scope of a parameter declared in an indexer-declaration (?7.8) is

the accessor-declarations of that

indexer-declaration.

?The scope of a parameter declared in an operator-declaration (?7.9) is

the block of that operatordeclaration.

?The scope of a parameter declared in a constructor-declaration (?7.10)

is the constructor-initializer

and block of that constructor-declaration.

?The scope of a label declared in a labeled-statement (?5.4) is the block

in which the declaration occurs.

?The scope of a local variable declared in a local-variable-declaration (?5

.5.1) is the block in which the

declaration occurs.

?The scope of a local variable declared in a switch-block of a switch

statement (?5.7.2) is the switchblock.

?The scope of a local variable declared in a for-initializer of a for

statement (?5.8.3) is the forinitializer,

the for-condition, the for-iterator, and the contained statement of the for

statement.

?The scope of a local constant declared in a local-constant-declaration (?5

.5.2) is the block in which the

declaration occurs. It is a compile-time error to refer to a local constant

in a textual position that

precedes its constant-declarator.

Within the scope of a namespace, class, struct, or enumeration member it is

possible to refer to the member

in a textual position that precedes the declaration of the member.

[Example: For example

class A

{

void F() {

i = 1;

}

int i = 0;

}

Here, it is valid for F to refer to i before it is declared. end example]

Within the scope of a local variable, it is a compile-time error to refer

to the local variable in a textual

position that precedes the local-variable-declarator of the local variable.

[Example: For example

class A

{

int i = 0;

void F() {

i = 1; // Error, use precedes declaration

int i;

i = 2;

}

void G() {

int j = (j = 1); // Valid

}

void H() {

int a = 1, b = ++a; // Valid

}

}

Chapter 10 Basic concepts

81

In the F method above, the first assignment to i specifically does not

refer to the field declared in the outer

scope. Rather, it refers to the local variable and it results in a

compile-time error because it textually

precedes the declaration of the variable. In the G method, the use of j in

the initializer for the declaration of

j is valid because the use does not precede the local-variable-declarator.

In the H method, a subsequent

local-variable-declarator correctly refers to a local variable declared in

an earlier local-variable-declarator

within the same local-variable-declaration. end example]

[Note: The scoping rules for local variables are designed to guarantee that

the meaning of a name used in an

expression context is always the same within a block. If the scope of a

local variable were to extend only

from its declaration to the end of the block, then in the example above,

the first assignment would assign to

the instance variable and the second assignment would assign to the local

variable. (In certain situations, but

not in the example above, this could lead to a compile-time error if the

statements of the block were later to

be rearranged.)

The meaning of a name within a block may differ based on the context in

which the name is used. In the

example

using System;

class A {}

class Test

{

static void Main() {

string A = "hello, world";

string s = A; // expression context

Type t = typeof(A); // type context

Console.WriteLine(s); // writes "hello, world"

Console.WriteLine(t.ToString()); // writes "Type: A"

}

}

the name A is used in an expression context to refer to the local variable

A and in a type context to refer to

the class A. end note]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有