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.

156 ❘ ChaPTer 7 OperAtOrs And cAsts<br />

This will display the number 4 , because an int is 4 bytes long.<br />

If you are using the sizeof operator with complex types (<strong>and</strong> not primitive types), you will need to block<br />

the code within an unsafe block as illustrated here:<br />

unsafe<br />

{<br />

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

}<br />

Chapter 13 looks at unsafe code in more detail.<br />

The typeof operator<br />

The typeof operator returns a System.Type object representing a specifi ed type. For example,<br />

typeof(string) will return a Type object representing the System.String type. This is useful when you<br />

want to use refl ection to fi nd information about an object dynamically. Chapter 14, “ Refl ection, ” looks at<br />

refl ection.<br />

nullable Types <strong>and</strong> operators<br />

Looking at the Boolean type, you have a true or false value that you can assign to this type. However, what<br />

if you wanted to defi ne the value of the type as undefi ned This is where using nullable types can have a<br />

distinct value to your applications. If you use nullable types in your programs, you must always consider<br />

the effect a null value can have when used in conjunction with the various operators. Usually, when using a<br />

unary or binary operator with nullable types, the result will be null if one or both of the oper<strong>and</strong>s is null .<br />

For example:<br />

int a = null;<br />

int b = a + 4;<br />

int c = a * 5;<br />

// b = null<br />

// c = null<br />

However, when comparing nullable types, if only one of the oper<strong>and</strong>s is null , the comparison will always<br />

equate to false . This means that you cannot assume a condition is true just because its opposite is false ,<br />

as often happens in programs using non - nullable types. For example:<br />

int a = null;<br />

int b = -5;<br />

if (a > = b)<br />

Console.WriteLine("a > = b");<br />

else<br />

Console.WriteLine("a < b");<br />

The possibility of a null value means that you cannot freely combine nullable <strong>and</strong><br />

non-nullable types in an expression. This is discussed in the “Type Conversions”<br />

section later in this chapter.<br />

The null Coalescing operator<br />

The null coalescing operator ( ) provides a shorth<strong>and</strong> mechanism to cater to the possibility of null values<br />

when working with nullable <strong>and</strong> reference types. The operator is placed between two oper<strong>and</strong>s — the fi rst<br />

oper<strong>and</strong> must be a nullable type or reference type, <strong>and</strong> the second oper<strong>and</strong> must be of the same type as the<br />

fi r s t or of a t y p e t h at i s i mpl ic it ly c onver t ible to t he t y p e of t he fi r s t op era nd . T he nu l l c oa le s c i ng op erator<br />

evaluates as follows:<br />

➤<br />

➤<br />

If the fi rst oper<strong>and</strong> is not null , then the overall expression has the value of the fi rst oper<strong>and</strong>.<br />

If the fi rst oper<strong>and</strong> is null , then the overall expression has the value of the second oper<strong>and</strong>.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!