13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATIONand instance fields are two of the several kinds of variables (§12) supported by <strong>C#</strong>, and at times they are referredto 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 howmany instances of a class are created, there is only ever one copy of a static field for the associated applicationdomain.An instance field belongs to an instance. Specifically, every instance of a class contains a separate set of all theinstance 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 atype 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 fieldsWhen a field-declaration includes a readonly modifier, the fields introduced by the declaration are readonlyfields. Direct assignments to readonly fields can only occur as part of that declaration or in an instanceconstructor or static constructor in the same class. (A readonly field can be assigned to multiple times in thesecontexts.) 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 staticfield, in the static constructor of the class that contains the field declaration. These are also the only contextsin 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 acompile-time error.17.4.2.1 Using static readonly fields for constantsA static readonly field is useful when a symbolic name for a constant value is desired, but when the type ofthe value is not permitted in a const declaration, or when the value cannot be computed at compile-time.[Example: In the examplepublic class Color{public static readonly Color Black = new Color(0, 0, 0);public static readonly Color White = new Color(255, 255, 255);public static readonly Color Red = new Color(255, 0, 0);public static readonly Color Green = new Color(0, 255, 0);public static readonly Color Blue = new Color(0, 0, 255);private byte red, green, blue;public Color(byte r, byte g, byte b) {red = r;green = g;blue = b;}}the Black, White, Red, Green, and Blue members cannot be declared as const members because their valuescannot be computed at compile-time. However, declaring them static readonly instead has much the sameeffect. end example]17.4.2.2 Versioning of constants and static readonly fieldsConstants and readonly fields have different binary versioning semantics. When an expression references aconstant, the value of the constant is obtained at compile-time, but when an expression references a readonlyfield, the value of the field is not obtained until run-time. [Example: Consider an application that consists of twoseparate programs:222

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!