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.

o p e r a t o r s ❘ 155<br />

When you try to run this code, you will get an error message like this:<br />

Unh<strong>and</strong>led Exception: System.OverflowException: Arithmetic operation resulted in an<br />

overflow at Wrox.ProCSharp.Basics.OverflowTest.Main(String[] args)<br />

You can enforce overfl ow checking for all unmarked code in your program by<br />

specifying the /checked compiler option.<br />

If you want to suppress overfl ow checking, you can mark the code as unchecked :<br />

byte b = 255;<br />

unchecked<br />

{<br />

b++;<br />

}<br />

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

In this case, no exception will be raised, but you will lose data — because the byte type cannot hold a value<br />

of 256, the overfl owing bits will be discarded, <strong>and</strong> your b variable will hold a value of zero (0).<br />

Note that unchecked is the default behavior. The only time you are likely to need to explicitly use the<br />

unchecked keyword is if you need a few unchecked lines of code inside a larger block that you have<br />

explicitly marked as checked .<br />

The is operator<br />

The is operator allows you to check whether an object is compatible with a specifi c type. The phrase<br />

“ is compatible ” means that an object either is of that type or is derived from that type. For example, to<br />

check whether a variable is compatible with the object type, you could use the following bit of code:<br />

int i = 10;<br />

if (i is object)<br />

{<br />

Console.WriteLine("i is an object");<br />

}<br />

int , like all <strong>C#</strong> data types, inherits from object ; therefore, the expression i is object will evaluate to<br />

true in this case, <strong>and</strong> the appropriate message will be displayed.<br />

The as operator<br />

The as operator is used to perform explicit type conversions of reference types. If the type being converted<br />

is compatible with the specifi ed type, conversion is performed successfully. However, if the types are<br />

incompatible, the as operator returns the value null . As shown in the following code, attempting to convert<br />

an object reference to a string will return null if the object reference does not actually refer to a<br />

string instance:<br />

object o1 = "Some String";<br />

object o2 = 5;<br />

string s1 = o1 as string; // s1 = "Some String"<br />

string s2 = o2 as string; // s2 = null<br />

The as operator allows you to perform a safe type conversion in a single step without the need to fi rst<br />

test the type using the is operator <strong>and</strong> then perform the conversion.<br />

The sizeof operator<br />

You can determine the size (in bytes) required on the stack by a value type using the sizeof operator:<br />

Console.WriteLine(sizeof(int));<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!