A field is a member that represents a variable associated with an object or class. A field-declaration introduces one or more fields of a given type. field-declaration: attributesopt field-modifiersopt type variable-declarators ; field-modifiers: field-modifier field-modifiers field-modifier Chapter 17 Classes 221 field-modifier: new public protected internal private static readonly volatile variable-declarators: variable-declarator variable-declarators , variable-declarator variable-declarator: identifier identifier = variable-initializer variable-initializer: expression array-initializer A field-declaration may include a set of attributes (§24), a new modifier ( §17.2.2), a valid combination of the four access modifiers (§17.2.3), and a static modifier (§17.4.1). In addition, a field-declaration may include a readonly modifier (§17.4.2) or a volatile modifier (§17.4.3), but not both The attributes and modifiers apply to all of the members declared by the field-declaration. It is an error for the same modifier to appear multiple times in a field declaration. The type of a field-declaration specifies the type of the members introduced by the declaration. The type is followed by a list of variable-declarators, each of which introduces a new member. A variable-declarator consists of an identifier that names that member, optionally followed by an ?=? token and a variable-initializer (§17.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 (§10. 5.4). The value of a field is obtained in an expression using a simple-name (§14.5 .2) or a member-access (§14.5.4). The value of a non-readonly field is modified using an assignment (§14.13). The value of a non-readonly field can be both obtained and modified using postfix increment and decrement operators (§14.5.9) and prefix increment and decrement operators (§14.6.5). A field declaration that declares multiple fields is equivalent to multiple declarations of single fields with the same attributes, modifiers, and type. [Example: 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; } end example] 17.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 C# LANGUAGE SPECIFICATION 222 and instance fields are two of the several kinds of variables (§12) 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 (§14.5.4) of the form E.M, if M is a static field, E must denote a type that has a field M, and if M is an instance field, E must denote an instance of a type that has a field M. The differences between static and instance members are discussed further in §17.2.5. 17.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: ?