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.

152 ❘ ChaPTer 7 OperAtOrs And cAsts<br />

(continued)<br />

CaTegory<br />

oPeraTor<br />

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

Member access (for objects <strong>and</strong> structs) .<br />

Indexing (for arrays <strong>and</strong> indexers) []<br />

Cast ()<br />

Conditional (the ternary operator) :<br />

Delegate concatenation <strong>and</strong> removal (discussed in<br />

Chapter 8, “Delegates, Lambdas, <strong>and</strong> Events”)<br />

Object creation<br />

Type information<br />

Overflow exception control<br />

Indirection <strong>and</strong> address []<br />

Namespace alias qualifier (discussed in Chapter 2,<br />

“Core <strong>C#</strong>”)<br />

Null coalescing operator <br />

Note that four specific operators (sizeof, *, ->, <strong>and</strong> &, listed in the following table), however, are available<br />

only in unsafe code (code that bypasses <strong>C#</strong>’s type-safety checking), which is discussed in Chapter 13,<br />

“Memory Management <strong>and</strong> Pointers.” It is also important to note that the sizeof operator keywords, when<br />

used with the very early versions of the .<strong>NET</strong> Framework 1.0 <strong>and</strong> 1.1, required the unsafe mode. This is not<br />

a requirement since the .<strong>NET</strong> Framework 2.0 version.<br />

+ -<br />

new<br />

sizeof is typeof as<br />

checked unchecked<br />

::<br />

CaTegory<br />

Operator keywords<br />

Operators<br />

oPeraTor<br />

sizeof (for .<strong>NET</strong> Framework versions 1.0 <strong>and</strong> 1.1 only)<br />

* -> &<br />

One of the biggest pitfalls to watch out for when using <strong>C#</strong> operators is that, as with other C-style languages,<br />

<strong>C#</strong> uses different operators for assignment (=) <strong>and</strong> comparison (==). For instance, the following statement<br />

means let x equal three:<br />

x = 3;<br />

If you now want to compare x to a value, you need to use the double equals sign ==:<br />

if (x == 3)<br />

{<br />

}<br />

Fortunately, <strong>C#</strong>’s strict type-safety rules prevent the very common C error where assignment is performed<br />

instead of comparison in logical statements. This means that in <strong>C#</strong> the following statement will generate a<br />

compiler error:<br />

if (x = 3)<br />

{<br />

}<br />

Visual Basic programmers who are accustomed to using the ampers<strong>and</strong> (&) character to concatenate strings<br />

will have to make an adjustment. In <strong>C#</strong>, the plus sign (+) is used instead for concatenation, whereas the &<br />

symbol denotes a bitwise AND between two different integer values. The symbol | allows you to perform<br />

a bitwise OR between two integers. Visual Basic programmers also might not recognize the modulus (%)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!