15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

154 ❘ ChaPTer 7 OperAtOrs And cAsts<br />

The prefix <strong>and</strong> postfix operators --x <strong>and</strong> x-- behave in the same way, but decrement rather than<br />

increment the oper<strong>and</strong>.<br />

The other shortcut operators, such as += <strong>and</strong> -=, require two oper<strong>and</strong>s, <strong>and</strong> are used to modify the value<br />

of the first oper<strong>and</strong> by performing an arithmetic, logical, or bitwise operation on it. For example, the next<br />

two lines are equivalent:<br />

x += 5;<br />

x = x + 5;<br />

The following sections look at some of the primary <strong>and</strong> cast operators that you will frequently use within<br />

your <strong>C#</strong> code.<br />

The Conditional operator<br />

The conditional operator (:), also known as the ternary operator, is a shorth<strong>and</strong> form of the if...else<br />

construction. It gets its name from the fact that it involves three oper<strong>and</strong>s. It allows you to evaluate a<br />

condition, returning one value if that condition is true, or another value if it is false. The syntax is<br />

condition true_value: false_value<br />

Here, condition is the Boolean expression to be evaluated, true_value is the value that will be returned if<br />

condition is true, <strong>and</strong> false_value is the value that will be returned otherwise.<br />

When used sparingly, the conditional operator can add a dash of terseness to your programs. It is especially<br />

h<strong>and</strong>y for providing one of a couple of arguments to a function that is being invoked. You can use it to<br />

quickly convert a Boolean value to a string value of true or false. It is also h<strong>and</strong>y for displaying the<br />

correct singular or plural form of a word, for example:<br />

int x = 1;<br />

string s = x + " ";<br />

s += (x == 1 "man": "men");<br />

Console.WriteLine(s);<br />

This code displays 1 man if x is equal to one but will display the correct plural form for any other number.<br />

Note, however, that if your output needs to be localized to different languages, you will have to write more<br />

sophisticated routines to take into account the different grammatical rules of different languages.<br />

The checked <strong>and</strong> unchecked operators<br />

Consider the following code:<br />

byte b = 255;<br />

b++;<br />

Console.WriteLine(b.ToString());<br />

The byte data type can hold values only in the range 0 to 255, so incrementing the value of b causes<br />

an overflow. How the CLR h<strong>and</strong>les this depends on a number of issues, including compiler options, so<br />

whenever there’s a risk of an unintentional overflow, you need some way of making sure that you get the<br />

result you want.<br />

To do this, <strong>C#</strong> provides the checked <strong>and</strong> unchecked operators. If you mark a block of code as checked, the<br />

CLR will enforce overflow checking, <strong>and</strong> throw an OverflowException if an overflow occurs. Let’s change<br />

the code to include the checked operator:<br />

byte b = 255;<br />

checked<br />

{<br />

b++;<br />

}<br />

Console.WriteLine(b.ToString());<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!