03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

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

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

Delegates ❘ 93<br />

To determine whether a nullable variable contains a value, either use its HasValue property or simply<br />

compare it to null. The following code determines whether the variable count holds a value in a<br />

couple different ways.<br />

if (count.HasValue) Console.WriteLine("count = " + count);<br />

else Console.WriteLine("count = null");<br />

if (count != null) Console.WriteLine("count = " + count);<br />

if (count == null) Console.WriteLine("count = null");<br />

The first line of code uses the variable’s HasValue property. If count has a value, the code displays it.<br />

The else statement on the next line displays “count = null” if the variable has no value.<br />

The next two lines use != and == to compare the variable to the value null.<br />

The syntax used by nullable variables makes them remarkably easy to use and understand. There’s<br />

only one slightly complex issue: null propagation. When you perform a calculation with a nullable<br />

variable that holds no value, its “nullness” propagates into the result. For example, if a nullable integer<br />

contains no value, it probably doesn’t make sense to add another number to it. (What is null + 11)<br />

If any of the operands in an expression contains a null value, the result is a null value. Any time<br />

you need to use a nullable value, you should first check to see whether it is null or a usable value.<br />

Delegates<br />

A delegate is a special data type that refers to a method. The method can be an instance method<br />

provided by an object or a static method defined by a class. Like other types such as classes and<br />

enumerations, delegates can be defined in namespaces and classes but not inside methods.<br />

A delegate variable acts as a pointer to a method. Delegate variables are sometimes called type-safe<br />

function pointers.<br />

The delegate keyword defines a delegate type and specifies the parameters and return type of the<br />

method to which the delegate refers.<br />

After you have defined a delegate type, you can declare variables that are of that type. You can set<br />

those variables equal to methods that match the delegate’s parameters and return value.<br />

For example, the following code defines a delegate type named ShowMessageType. This delegate<br />

represents methods that take a single string as a parameter and that don’t return any result.<br />

private delegate void ShowMessageType(string msg);<br />

The following defines a ShowInConsoleWindow method, which simply displays a message in the<br />

Console window.<br />

// Display a message in the Console window.<br />

private void ShowInConsoleWindow(string message)<br />

{<br />

Console.WriteLine(message);<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!