分享
 
 
 

12.1 Variable categories

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

parameters, reference parameters, output parameters, and local variables.

The sections that follow describe

each of these categories.

[Example: In the example

class A

{

public static int x;

int y;

void F(int[] v, int a, ref int b, out int c) {

int i = 1;

c = a + b++;

}

}

x is a static variable, y is an instance variable, v[0] is an array element,

a is a value parameter, b is a

reference parameter, c is an output parameter, and i is a local variable.

end example]

12.1.1 Static variables

A field declared with the static modifier is called a static variable. A

static variable comes into existence

before execution of the static constructor (§17.11) for its containing

type, and ceases to exist when the

associated application domain ceases to exist.

The initial value of a static variable is the default value (§12.2) of the

variable.s type.

For the purposes of definite assignment checking, a static variable is

considered initially assigned.

12.1.2 Instance variables

A field declared without the static modifier is called an instance variable.

12.1.2.1 Instance variables in classes

An instance variable of a class comes into existence when a new instance of

that class is created, and ceases

to exist when there are no references to that instance and the instance.s

destructor (if any) has executed.

The initial value of an instance variable of a class is the default value (§

12.2) of the variable.s type.

For the purpose of definite assignment checking, an instance variable is

considered initially assigned.

C# LANGUAGE SPECIFICATION

100

12.1.2.2 Instance variables in structs

An instance variable of a struct has exactly the same lifetime as the

struct variable to which it belongs. In

other words, when a variable of a struct type comes into existence or

ceases to exist, so too do the instance

variables of the struct.

The initial assignment state of an instance variable of a struct is the

same as that of the containing struct

variable. In other words, when a struct variable is considered initially

assigned, so too are its instance

variables, and when a struct variable is considered initially unassigned,

its instance variables are likewise

unassigned.

12.1.3 Array elements

The elements of an array come into existence when an array instance is

created, and cease to exist when

there are no references to that array instance.

The initial value of each of the elements of an array is the default value (

§12.2) of the type of the array

elements.

For the purpose of definite assignment checking, an array element is

considered initially assigned.

12.1.4 Value parameters

A parameter declared without a ref or out modifier is a value parameter.

A value parameter comes into existence upon invocation of the function

member (method, instance

constructor, accessor, or operator) to which the parameter belongs, and is

initialized with the value of the

argument given in the invocation. A value parameter ceases to exist upon

return of the function member.

For the purpose of definite assignment checking, a value parameter is

considered initially assigned.

12.1.5 Reference parameters

A parameter declared with a ref modifier is a reference parameter.

A reference parameter does not create a new storage location. Instead, a

reference parameter represents the

same storage location as the variable given as the argument in the function

member invocation. Thus, the

value of a reference parameter is always the same as the underlying

variable.

The following definite assignment rules apply to reference parameters.

[Note: The rules for output

parameters are different, and are described in §12.1.6. end note]

. A variable must be definitely assigned (§12.3) before it can be passed

as a reference parameter in a

function member invocation.

. Within a function member, a reference parameter is considered initially

assigned.

Within an instance method or instance accessor of a struct type, the this

keyword behaves exactly as a

reference parameter of the struct type (§14.5.7).

12.1.6 Output parameters

A parameter declared with an out modifier is an output parameter.

An output parameter does not create a new storage location. Instead, an

output parameter represents the

same storage location as the variable given as the argument in the function

member invocation. Thus, the

value of an output parameter is always the same as the underlying variable.

The following definite assignment rules apply to output parameters. [Note:

The rules for reference

parameters are different, and are described in §12.1.5. end note]

. A variable need not be definitely assigned before it can be passed as an

output parameter in a function

member invocation.

Chapter 12 Variables

101

. Following the normal completion of a function member invocation, each

variable that was passed as an

output parameter is considered assigned in that execution path.

. Within a function member, an output parameter is considered initially

unassigned.

. Every output parameter of a function member must be definitely assigned (§

12.3) before the function

member returns normally.

Within an instance constructor of a struct type, the this keyword behaves

exactly as an output parameter of

the struct type (§14.5.7).

12.1.7 Local variables

A local variable is declared by a local-variable-declaration, which may

occur in a block, a for-statement, a

switch-statement, or a using-statement.

The lifetime of a local variable is the portion of program execution during

which storage is guaranteed to be

reserved for it. This lifetime extends from entry into the block,

for-statement, switch-statement, or usingstatement

with which it is associated, until execution of that block, for-statement,

switch-statement, or usingstatement

ends in any way. (Entering an enclosed block or calling a method suspends,

but does not end,

execution of the current block, for-statement, switch-statement, or

using-statement.) If the parent block, forstatement,

switch-statement, or using-statement is entered recursively, a new instance

of the local variable is

created each time, and its local-variable-initializer, if any, is evaluated

each time.

A local variable is not automatically initialized and thus has no default

value. For the purpose of definite

assignment checking, a local variable is considered initially unassigned. A

local-variable-declaration may

include a local-variable-initializer, in which case the variable is

considered definitely assigned in its entire

scope, except within the expression provided in the

local-variable-initializer.

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

to that local variable in a textual

position that precedes its local-variable-declarator.

[Note: The actual lifetime of a local variable is implementation-dependent.

For example, a compiler might

statically determine that a local variable in a block is only used for a

small portion of that block. Using this

analysis, the compiler could generate code that results in the variable.s

storage having a shorter lifetime than

its containing block.

The storage referred to by a local reference variable is reclaimed

independently of the lifetime of that local

reference variable (§10.9). end note]

A local variable is also declared by a foreach-statement and by a

specific-catch-clause for a try-statement.

For a foreach-statement, the local variable is an iteration variable (§15.8.

4). For a specific-catch-clause, the

local variable is an exception variable (§15.10). A local variable

declared by a foreach-statement or specificcatch-

clause is considered definitely assigned in its entire scope.

parameters, reference parameters, output parameters, and local variables.

The sections that follow describe

each of these categories.

[Example: In the example

class A

{

public static int x;

int y;

void F(int[] v, int a, ref int b, out int c) {

int i = 1;

c = a + b++;

}

}

x is a static variable, y is an instance variable, v[0] is an array element,

a is a value parameter, b is a

reference parameter, c is an output parameter, and i is a local variable.

end example]

12.1.1 Static variables

A field declared with the static modifier is called a static variable. A

static variable comes into existence

before execution of the static constructor (§17.11) for its containing

type, and ceases to exist when the

associated application domain ceases to exist.

The initial value of a static variable is the default value (§12.2) of the

variable.s type.

For the purposes of definite assignment checking, a static variable is

considered initially assigned.

12.1.2 Instance variables

A field declared without the static modifier is called an instance variable.

12.1.2.1 Instance variables in classes

An instance variable of a class comes into existence when a new instance of

that class is created, and ceases

to exist when there are no references to that instance and the instance.s

destructor (if any) has executed.

The initial value of an instance variable of a class is the default value (§

12.2) of the variable.s type.

For the purpose of definite assignment checking, an instance variable is

considered initially assigned.

C# LANGUAGE SPECIFICATION

100

12.1.2.2 Instance variables in structs

An instance variable of a struct has exactly the same lifetime as the

struct variable to which it belongs. In

other words, when a variable of a struct type comes into existence or

ceases to exist, so too do the instance

variables of the struct.

The initial assignment state of an instance variable of a struct is the

same as that of the containing struct

variable. In other words, when a struct variable is considered initially

assigned, so too are its instance

variables, and when a struct variable is considered initially unassigned,

its instance variables are likewise

unassigned.

12.1.3 Array elements

The elements of an array come into existence when an array instance is

created, and cease to exist when

there are no references to that array instance.

The initial value of each of the elements of an array is the default value (

§12.2) of the type of the array

elements.

For the purpose of definite assignment checking, an array element is

considered initially assigned.

12.1.4 Value parameters

A parameter declared without a ref or out modifier is a value parameter.

A value parameter comes into existence upon invocation of the function

member (method, instance

constructor, accessor, or operator) to which the parameter belongs, and is

initialized with the value of the

argument given in the invocation. A value parameter ceases to exist upon

return of the function member.

For the purpose of definite assignment checking, a value parameter is

considered initially assigned.

12.1.5 Reference parameters

A parameter declared with a ref modifier is a reference parameter.

A reference parameter does not create a new storage location. Instead, a

reference parameter represents the

same storage location as the variable given as the argument in the function

member invocation. Thus, the

value of a reference parameter is always the same as the underlying

variable.

The following definite assignment rules apply to reference parameters.

[Note: The rules for output

parameters are different, and are described in §12.1.6. end note]

. A variable must be definitely assigned (§12.3) before it can be passed

as a reference parameter in a

function member invocation.

. Within a function member, a reference parameter is considered initially

assigned.

Within an instance method or instance accessor of a struct type, the this

keyword behaves exactly as a

reference parameter of the struct type (§14.5.7).

12.1.6 Output parameters

A parameter declared with an out modifier is an output parameter.

An output parameter does not create a new storage location. Instead, an

output parameter represents the

same storage location as the variable given as the argument in the function

member invocation. Thus, the

value of an output parameter is always the same as the underlying variable.

The following definite assignment rules apply to output parameters. [Note:

The rules for reference

parameters are different, and are described in §12.1.5. end note]

. A variable need not be definitely assigned before it can be passed as an

output parameter in a function

member invocation.

Chapter 12 Variables

101

. Following the normal completion of a function member invocation, each

variable that was passed as an

output parameter is considered assigned in that execution path.

. Within a function member, an output parameter is considered initially

unassigned.

. Every output parameter of a function member must be definitely assigned (§

12.3) before the function

member returns normally.

Within an instance constructor of a struct type, the this keyword behaves

exactly as an output parameter of

the struct type (§14.5.7).

12.1.7 Local variables

A local variable is declared by a local-variable-declaration, which may

occur in a block, a for-statement, a

switch-statement, or a using-statement.

The lifetime of a local variable is the portion of program execution during

which storage is guaranteed to be

reserved for it. This lifetime extends from entry into the block,

for-statement, switch-statement, or usingstatement

with which it is associated, until execution of that block, for-statement,

switch-statement, or usingstatement

ends in any way. (Entering an enclosed block or calling a method suspends,

but does not end,

execution of the current block, for-statement, switch-statement, or

using-statement.) If the parent block, forstatement,

switch-statement, or using-statement is entered recursively, a new instance

of the local variable is

created each time, and its local-variable-initializer, if any, is evaluated

each time.

A local variable is not automatically initialized and thus has no default

value. For the purpose of definite

assignment checking, a local variable is considered initially unassigned. A

local-variable-declaration may

include a local-variable-initializer, in which case the variable is

considered definitely assigned in its entire

scope, except within the expression provided in the

local-variable-initializer.

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

to that local variable in a textual

position that precedes its local-variable-declarator.

[Note: The actual lifetime of a local variable is implementation-dependent.

For example, a compiler might

statically determine that a local variable in a block is only used for a

small portion of that block. Using this

analysis, the compiler could generate code that results in the variable.s

storage having a shorter lifetime than

its containing block.

The storage referred to by a local reference variable is reclaimed

independently of the lifetime of that local

reference variable (§10.9). end note]

A local variable is also declared by a foreach-statement and by a

specific-catch-clause for a try-statement.

For a foreach-statement, the local variable is an iteration variable (§15.8.

4). For a specific-catch-clause, the

local variable is an exception variable (§15.10). A local variable

declared by a foreach-statement or specificcatch-

clause is considered definitely assigned in its entire scope.

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