X hits on this document





96 / 396

Chapter ‎18   Unsafe code

5. Variables

Variables represent storage locations. Every variable has a type that determines what values can be stored in the variable. C# is a type-safe language, and the C# compiler guarantees that values stored in variables are always of the appropriate type. The value of a variable can be changed through assignment or through use of the ++ and ‑‑ operators.

A variable must be definitely assigned (§‎5.3) before its value can be obtained.

As described in the following sections, variables are either initially assigned or initially unassigned. An initially assigned variable has a well-defined initial value and is always considered definitely assigned. An initially unassigned variable has no initial value. For an initially unassigned variable to be considered definitely assigned at a certain location, an assignment to the variable must occur in every possible execution path leading to that location.

5.1 Variable categories

C# defines seven categories of variables: static variables, instance variables, array elements, value parameters, reference parameters, output parameters, and local variables. The sections that follow describe each of these categories.

In the example

class 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 a reference parameter, c is an output parameter, and i is a local variable.

5.1.1 Static variables

A field declared with the static modifier is called a static variable. A static variable comes into existence before execution of the static constructor (§‎10.11) for its containing type, and ceases to exist when the associated application domain ceases to exist.

The initial value of a static variable is the default value (§‎5.2) of the variable’s type.

For purposes of definite assignment checking, a static variable is considered initially assigned.

5.1.2 Instance variables

A field declared without the static modifier is called an instance variable. Instance variables in classes

An instance variable of a class comes into existence when a new instance of that class is created, and ceases to exist when there are no references to that instance and the instance’s destructor (if any) has executed.

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

Document info
Document views610
Page views610
Page last viewedWed Oct 26 07:51:10 UTC 2016