13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

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

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

Delegates35FINDING AN APPROPRIATE METHOD FOR THE DELEGATE INSTANCE’S ACTIONIn .NET, delegate <strong>in</strong>stances always refer to methods. Our next <strong>in</strong>gredient is to f<strong>in</strong>d (orwrite, of course) a method that does what we want and has the same signature as the delegatetype we’re us<strong>in</strong>g. The idea is to make sure that when we try to <strong>in</strong>voke a delegate<strong>in</strong>stance, the parameters we use will all match up and we’ll be able to use the return value(if any) <strong>in</strong> the way we expect—just like a normal method call.Now consider these five method signatures as candidates to be used for a Str<strong>in</strong>g-Processor <strong>in</strong>stance:void Pr<strong>in</strong>tStr<strong>in</strong>g (str<strong>in</strong>g x)void Pr<strong>in</strong>tInteger (<strong>in</strong>t x)void Pr<strong>in</strong>tTwoStr<strong>in</strong>gs (str<strong>in</strong>g x, str<strong>in</strong>g y)<strong>in</strong>t GetStr<strong>in</strong>gLength (str<strong>in</strong>g x)void Pr<strong>in</strong>tObject (object x)The first method has everyth<strong>in</strong>g right, so we can use it to create a delegate <strong>in</strong>stance.The second method takes one parameter, but it’s not str<strong>in</strong>g, so it’s <strong>in</strong>compatible withStr<strong>in</strong>gProcessor. The third method has the correct first parameter type, but it hasanother parameter as well, so it’s still <strong>in</strong>compatible.The fourth method has the right parameter list but a nonvoid return type. (If ourdelegate type had a return type, the return type of the method would have to matchthat too.) The fifth method is <strong>in</strong>terest<strong>in</strong>g—any time we <strong>in</strong>voke a Str<strong>in</strong>gProcessor<strong>in</strong>stance we could call the Pr<strong>in</strong>tObject method with the same arguments, becausestr<strong>in</strong>g derives from object. It would make sense to be able to use it for an <strong>in</strong>stance ofStr<strong>in</strong>gProcessor, but <strong>C#</strong> 1 limits the delegate to have exactly the same parametertypes. 1 <strong>C#</strong> 2 changes this situation—see chapter 5 for more details. In some ways thefourth method is similar, as you could always ignore the unwanted return value. However,void and nonvoid return types are currently always deemed to be <strong>in</strong>compatible.Let’s assume we’ve got a method body for the compatible signature (Pr<strong>in</strong>tStr<strong>in</strong>g)and move on to our next <strong>in</strong>gredient—the delegate <strong>in</strong>stance itself.CREATING A DELEGATE INSTANCENow that we’ve got a delegate type and a method with the right signature, we can createan <strong>in</strong>stance of that delegate type, specify<strong>in</strong>g that this method be executed when the delegate<strong>in</strong>stance is <strong>in</strong>voked. There’s no good official term<strong>in</strong>ology def<strong>in</strong>ed for this, but forthis book I will call it the action of the delegate <strong>in</strong>stance. The exact form of the expressionused to create the delegate <strong>in</strong>stance depends on whether the action uses an <strong>in</strong>stancemethod or a static method. Suppose Pr<strong>in</strong>tStr<strong>in</strong>g is a static method <strong>in</strong> a type calledStaticMethods and an <strong>in</strong>stance method <strong>in</strong> a type called InstanceMethods. Here are twoexamples of creat<strong>in</strong>g an <strong>in</strong>stance of Str<strong>in</strong>gProcessor:Str<strong>in</strong>gProcessor proc1, proc2;proc1 = new Str<strong>in</strong>gProcessor(StaticMethods.Pr<strong>in</strong>tStr<strong>in</strong>g);InstanceMethods <strong>in</strong>stance = new InstanceMethods();proc2 = new Str<strong>in</strong>gProcessor(<strong>in</strong>stance.Pr<strong>in</strong>tStr<strong>in</strong>g);1As well as the parameter types, you have to match whether the parameter is <strong>in</strong> (the default), out, or ref. It’sreasonably rare to use out/ref parameters with delegates, though.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!