Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

10.4 Fields

Visual Studio .NET 2003

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
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 (Section 17), a new modifier (Section 10.2.2), a valid combination of the four access modifiers (Section 10.2.3), and a static modifier (Section 10.4.1). In addition, a field-declaration may include a readonly modifier (Section 10.4.2) or a volatile modifier (Section 10.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 (Section 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 (Section 3.5.4).

The value of a field is obtained in an expression using a simple-name (Section 7.5.2) or a member-access (Section 7.5.4). The value of a non-readonly field is modified using an assignment (Section 7.13). The value of a non-readonly field can be both obtained and modified using postfix increment and decrement operators (Section 7.5.9) and prefix increment and decrement operators (Section 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;
}
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft