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.

190 ❘ ChaPTer 8 deleGAtes, lAmbdAs, And events<br />

Then, when in this method, you call<br />

double result = action(value);<br />

this actually causes the method that is wrapped up by the action delegate instance to be called <strong>and</strong> its<br />

return result stored in Result. Running this example gives you the following:<br />

SimpleDelegate<br />

Using operations[0]:<br />

Value is 2, result of operation is 4<br />

Value is 7.94, result of operation is 15.88<br />

Value is 1.414, result of operation is 2.828<br />

Using operations[1]:<br />

Value is 2, result of operation is 4<br />

Value is 7.94, result of operation is 63.0436<br />

Value is 1.414, result of operation is 1.999396<br />

action <strong>and</strong> func delegates<br />

Instead of defining a new delegate type with every parameter <strong>and</strong> return type, you can use the Action<br />

<strong>and</strong> Func delegates. The generic Action delegate is meant to reference a method with void return.<br />

This delegate class exists in different variants so that you can pass up to 16 different parameter types. The<br />

Action class without the generic parameter is for calling methods without parameters. Action is<br />

to call a method with one parameter, Action for a method with two parameters, <strong>and</strong><br />

Action for a method with<br />

eight parameters.<br />

The Func delegates can be used in a similar manner. Func allows you to invoke methods with a<br />

return type. Similar to Action, Func is defined in different variants to pass up to 16 parameter types<br />

<strong>and</strong> a return type. Func is the delegate type to invoke a method with a return type <strong>and</strong><br />

without parameters. Func is for a method with one parameter, <strong>and</strong> Func is for a method with four parameters.<br />

The example in the preceding section declared a delegate with double parameter <strong>and</strong> double return type:<br />

delegate double DoubleOp(double x);<br />

Instead of declaring the custom delegate DoubleOp you can use the Func delegate.<br />

You can declare a variable of the delegate type, or as shown here an array of the delegate type:<br />

Func[] operations =<br />

{<br />

MathOperations.MultiplyByTwo,<br />

MathOperations.Square<br />

};<br />

<strong>and</strong> use it with the ProcessAndDisplayNumber() method as parameter:<br />

static void ProcessAndDisplayNumber(Func action,<br />

double value)<br />

{<br />

double result = action(value);<br />

Console.WriteLine(<br />

"Value is {0}, result of operation is {1}", value, result);<br />

}<br />

bubblesorter example<br />

You are now ready for an example that will show the real usefulness of delegates. You are going to write<br />

a class called BubbleSorter. This class implements a static method, Sort(), which takes as its first<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!