15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

Type safety ❘ 157<br />

For example:<br />

int a = null;<br />

int b;<br />

b = a 10; // b has the value 10<br />

a = 3;<br />

b = a 10; // b has the value 3<br />

If the second oper<strong>and</strong> cannot be implicitly converted to the type of the fi rst oper<strong>and</strong>, a compile - time error<br />

is generated.<br />

operator Precedence<br />

The following table shows the order of precedence of the <strong>C#</strong> operators. The operators at the top of the table<br />

are those with the highest precedence (that is, the ones evaluated fi rst in an expression containing multiple<br />

operators).<br />

grouP<br />

Primary<br />

Unary<br />

oPeraTors<br />

Multiplication/division * / %<br />

Addition/subtraction + -<br />

Bitwise shift operators ><br />

Relational<br />

Comparison == !=<br />

Bitwise AND<br />

Bitwise XOR<br />

Bitwise OR |<br />

Boolean AND<br />

() . [] x++ x-- new typeof sizeof checked unchecked<br />

+ — ! ~ ++x --x <strong>and</strong> casts<br />

< >= is as<br />

&<br />

^<br />

&&<br />

Boolean OR ||<br />

Conditional operator :<br />

Assignment = += -= *= /= %= &= |= ^= = >>>=<br />

In complex expressions, you should avoid relying on operator precedence to produce<br />

the correct result. Using parentheses to specify the order in which you want operators<br />

applied clarifi es your code <strong>and</strong> prevents potential confusion.<br />

TyPe safeTy<br />

Chapter 1, “ .<strong>NET</strong> Architecture, ” noted that the Intermediate Language (IL) enforces strong type safety<br />

upon its code. Strong typing enables many of the services provided by .<strong>NET</strong>, including security <strong>and</strong><br />

language interoperability. As you would expect from a language compiled into IL, <strong>C#</strong> is also strongly typed.<br />

Among other things, this means that data types are not always seamlessly interchangeable. This section<br />

looks at conversions between primitive types.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!