X hits on this document

842 views

0 shares

0 downloads

0 comments

223 / 396

C# LANGUAGE SPECIFICATION

consists of an identifier that names that member, optionally followed by an “=” token and a variable-initializer (§‎10.4.5) that gives the initial value of that member.

The type of a field must be at least as accessible as the field itself (§‎3.5.4).

The value of a field is obtained in an expression using a simple-name (§‎7.5.2) or a member-access (§‎7.5.4). The value of a non-readonly field is modified using an assignment (§‎7.13). The value of a non-readonly field can be both obtained and modified using postfix increment and decrement operators (§‎7.5.9) and prefix increment and decrement operators (§‎7.6.5).

A field declaration that declares multiple fields is equivalent to multiple declarations of single fields with the same attributes, modifiers, and type. For example

class A { public static int X = 1, Y, Z = 100; }

is equivalent to

class A { public static int X = 1; public static int Y; public static int Z = 100; }

10.4.1 Static and instance fields

When a field declaration includes a static modifier, the fields introduced by the declaration are static fields. When no static modifier is present, the fields introduced by the declaration are instance fields. Static fields and instance fields are two of the several kinds of variables (§‎5) supported by C#, and at times they are referred to as static variables and instance variables, respectively.

A static field is not part of a specific instance; instead, it identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field for the associated application domain.

An instance field belongs to an instance. Specifically, every instance of a class contains a separate set of all the instance fields of that class.

When a field is referenced in a member-access (§‎7.5.4) of the form E.M, if M is a static field, E must denote a type containing M, and if M is an instance field, E must denote an instance of a type containing M.

The differences between static and instance members are discussed further in §‎10.2.5.

10.4.2 Readonly fields

When a field-declaration includes a readonly modifier, the fields introduced by the declaration are readonly fields. Direct assignments to readonly fields can only occur as part of that declaration or in an instance constructor or static constructor in the same class. (A readonly field can be assigned to multiple times in these contexts.) Specifically, direct assignments to a readonly field are permitted only in the following contexts:

In the variable-declarator that introduces the field (by including a variable-initializer in the declaration).

For an instance field, in the instance constructors of the class that contains the field declaration; for a static field, in the static constructor of the class that contains the field declaration. These are also the only contexts in which it is valid to pass a readonly field as an out or ref parameter.

Attempting to assign to a readonly field or pass it as an out or ref parameter in any other context is a compile-time error.

210Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views842
Page views842
Page last viewedSun Dec 04 18:59:38 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments