13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12 Variables12. VariablesVariables represent storage locations. Every variable has a type that determines what values can be stored inthe variable. <strong>C#</strong> is a type-safe language, and the <strong>C#</strong> compiler guarantees that values stored in variables arealways of the appropriate type. The value of a variable can be changed through assignment or through use ofthe ++ and -- operators.A variable must be definitely assigned (§12.3) before its value can be obtained.As described in the following sections, variables are either initially assigned or initially unassigned. Aninitially assigned variable has a well-defined initial value and is always considered definitely assigned. Aninitially unassigned variable has no initial value. For an initially unassigned variable to be considereddefinitely assigned at a certain location, an assignment to the variable must occur in every possible executionpath leading to that location.12.1 Variable categories<strong>C#</strong> defines seven categories of variables: static variables, instance variables, array elements, valueparameters, reference parameters, output parameters, and local variables. The sections that follow describeeach of these categories.[Example: In the exampleclass 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 areference parameter, c is an output parameter, and i is a local variable. end example]12.1.1 Static variablesA field declared with the static modifier is called a static variable. A static variable comes into existencebefore execution of the static constructor (§17.11) for its containing type, and ceases to exist when theassociated 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 variablesA field declared without the static modifier is called an instance variable.12.1.2.1 Instance variables in classesAn instance variable of a class comes into existence when a new instance of that class is created, and ceasesto 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.99

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

Saved successfully!

Ooh no, something went wrong!