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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 ❘ 637<br />

The following code shows the King class.<br />

class King : Piece<br />

{<br />

public override bool CanMoveTo(int row, int column)<br />

{<br />

return true;<br />

}<br />

}<br />

9. The following statement defines the ManagersFromEmployeesDelegate type.<br />

delegate Manager[] ManagersFromEmployeesDelegate(Employee[] employees);<br />

The following code shows the Promote method that matches the delegate type.<br />

private Manager[] Promote(Employee[] employees)<br />

{<br />

return null;<br />

}<br />

The following statement creates a variable that holds a reference to the Promote method.<br />

ManagersFromEmployeesDelegate del = Promote;<br />

10. Covariance lets a method return a more derived type than the delegate. In this example,<br />

the delegate returns Manager[], so the new method should return the more derived type<br />

Executive[].<br />

Contravariance lets a method take parameters that are a less derived type than those taken<br />

by the delegate. In this example, the delegate takes an Employee[] as a parameter, so the<br />

new method should take the less derived parameter type Person[].<br />

The following code shows the new version of the Promote method.<br />

private Executive[] Promote2(Person[] people)<br />

{<br />

return null;<br />

}<br />

The following statement creates a variable that holds a reference to the new version of the<br />

Promote method.<br />

ManagersFromEmployeesDelegate del2 = Promote2;<br />

11. The ColorizeImage program, which is available for download on this book’s website, does<br />

this. Download the program to see how it works. In one test on my dual-core computer, processing<br />

an image took roughly 1.78 seconds synchronously and 0.92 seconds asynchronously.<br />

Because the computer has two cores, you might expect the asynchronous version to take<br />

one-half the time used by the synchronous version, but there is some overhead in setting<br />

up and coordinating the threads. The result is still an impressive reduction in time, however,<br />

and would be even greater on a computer with more cores.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!