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.

<strong>C#</strong> LANGUAGE SPECIFICATIONcompile-time error occurs if E is not classified as a variable, if E is classified as a volatile field (§17.4.3), orif E denotes a moveable variable. In the last case, a fixed statement (§25.6) can be used to temporarily “fix”the variable before obtaining its address.The & operator does not require its argument to be definitely assigned, but following an & operation, thevariable to which the operator is applied is considered definitely assigned in the execution path in which theoperation occurs. It is the responsibility of the programmer to ensure that correct initialization of the variableactually does take place in this situation.[Example: In the exampleusing System;class Test{static void Main() {int i;unsafe {int* p = &i;*p = 123;}Console.WriteLine(i);}}i is considered definitely assigned following the &i operation used to initialize p. The assignment to *p ineffect initializes i, but the inclusion of this initialization is the responsibility of the programmer, and nocompile-time error would occur if the assignment was removed. end example][Note: The rules of definite assignment for the & operator exist such that redundant initialization of localvariables can be avoided. For example, many external APIs take a pointer to a structure which is filled in bythe API. Calls to such APIs typically pass the address of a local struct variable, and without the rule,redundant initialization of the struct variable would be required. end note][Note: As stated in §14.5.4, outside an instance constructor or static constructor for a struct or class thatdefines a readonly field, that field is considered a value, not a variable. As such, its address cannot be taken.Similarly, the address of a constant cannot be taken. end note]25.5.5 Pointer increment and decrementIn an unsafe context, the ++ and -- operators (§14.5.9 and §14.6.5) can be applied to pointer variables of alltypes except void*. Thus, for every pointer type T*, the following operators are implicitly defined:T* operator ++(T* x);T* operator --(T* x);The operators produce the same results as x+1 and x-1, respectively (§25.5.6). In other words, for a pointervariable of type T*, the ++ operator adds sizeof(T) to the address contained in the variable, and the-- operator subtracts sizeof(T) from the address contained in the variable.If a pointer increment or decrement operation overflows the domain of the pointer type, the result isimplementation-defined, but no exceptions are produced.25.5.6 Pointer arithmeticIn an unsafe context, the + operator (§14.7.4) and – operator (§14.7.5) can be applied to values of allpointer types except void*. Thus, for every pointer type T*, the following operators are implicitly defined:T* operator +(T* x, int y);T* operator +(T* x, uint y);T* operator +(T* x, long y);T* operator +(T* x, ulong y);T* operator +(int x, T* y);T* operator +(uint x, T* y);T* operator +(long x, T* y);T* operator +(ulong x, T* y);326

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

Saved successfully!

Ooh no, something went wrong!