13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

140 CHAPTER 5 Fast-tracked delegates5.2 Method group conversionsIn <strong>C#</strong> 1, if you want to create a delegate <strong>in</strong>stance you need to specify both the delegatetype and the action. If you remember from chapter 2, we def<strong>in</strong>ed the action to be themethod to call and (for <strong>in</strong>stance methods) the target to call it on. So for example, <strong>in</strong> list<strong>in</strong>g5.1 when we needed to create a KeyPressEventHandler we used this expression:new KeyPressEventHandler(LogKeyEvent)As a stand-alone expression, it doesn’t look too bad. Even used <strong>in</strong> a simple event subscriptionit’s tolerable. It becomes a bit uglier when used as part of a longer expression.A common example of this is start<strong>in</strong>g a new thread:Thread t = new Thread (new ThreadStart(MyMethod));What we want to do is start a new thread that will execute MyMethod as simply as possible.<strong>C#</strong> 2 allows you to do this by means of an implicit conversion from a method groupto a compatible delegate type. A method group is simply the name of a method,optionally with a target—exactly the same k<strong>in</strong>d of expression as we used <strong>in</strong> <strong>C#</strong> 1 to createdelegate <strong>in</strong>stances, <strong>in</strong> other words. (Indeed, the expression was called a methodgroup back then—it’s just that the conversion wasn’t available.) If the method isgeneric, the method group may also specify type arguments. The new implicit conversionallows us to turn our event subscription <strong>in</strong>tobutton.KeyPress += LogKeyEvent;Likewise the thread creation code becomes simplyThread t = new Thread (MyMethod);The readability differences between the orig<strong>in</strong>al and the “streaml<strong>in</strong>ed” versions aren’thuge for a s<strong>in</strong>gle l<strong>in</strong>e, but <strong>in</strong> the context of a significant amount of code, they canreduce the clutter considerably. To make it look less like magic, let’s take a brief lookat what this conversion is do<strong>in</strong>g.First, let’s consider the expressions LogKeyEvent and MyMethod as they appear <strong>in</strong>the examples. The reason they’re classified as method groups is that more than onemethod may be available, due to overload<strong>in</strong>g. The implicit conversions available willconvert a method group to any delegate type with a compatible signature. So, if youhad two method signatures as follows:void MyMethod()void MyMethod(object sender, EventArgs e)you could use MyMethod as the method group <strong>in</strong> an assignment to either a ThreadStartor an EventHandler as follows:ThreadStart x = MyMethod;EventHandler y = MyMethod;However, you couldn’t use it as the parameter to a method that itself was overloaded totake either a ThreadStart or an EventHandler—the compiler would compla<strong>in</strong> thatthe conversion was ambiguous. Likewise, you unfortunately can’t use an implicitLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!