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.

38 CHAPTER 2 Core foundations: build<strong>in</strong>g on <strong>C#</strong> 1how they’re used differently when we create the delegate <strong>in</strong>stances E. We’ve createdtwo <strong>in</strong>stances of the Person class so that we can see the difference that the targetof a delegate makes. When jonsVoice is <strong>in</strong>voked F, it calls the Say method onthe Person object with the name Jon; likewise, when tomsVoice is <strong>in</strong>voked, it usesthe object with the name Tom. I’ve <strong>in</strong>cluded both the ways we’ve seen of <strong>in</strong>vok<strong>in</strong>gdelegate <strong>in</strong>stances—call<strong>in</strong>g Invoke explicitly and us<strong>in</strong>g the <strong>C#</strong> shorthand—just for<strong>in</strong>terest’s sake. Normally you’d just use the shorthand. The output for list<strong>in</strong>g 2.1 isfairly obvious:Jon says: Hello, son.Tom says: Hello, Daddy!(An airplane flies past.)Frankly, there’s an awful lot of code <strong>in</strong> list<strong>in</strong>g 2.1 to display three l<strong>in</strong>es of output. Evenif we wanted to use the Person class and the Background class, there’s no real need touse delegates here. So what’s the po<strong>in</strong>t? Why can’t we just call methods directly? Theanswer lies <strong>in</strong> our orig<strong>in</strong>al example of an attorney execut<strong>in</strong>g a will—just because youwant someth<strong>in</strong>g to happen, that doesn’t mean you’re always there at the right timeand place to make it happen yourself. Sometimes you need to give <strong>in</strong>structions—todelegate responsibility, as it were.I should stress that back <strong>in</strong> the world of software, this isn’t a matter ofobjects leav<strong>in</strong>g dy<strong>in</strong>g wishes. Often the object that first creates a delegate<strong>in</strong>stance is still alive and well when the delegate <strong>in</strong>stance is<strong>in</strong>voked. Instead, it’s about specify<strong>in</strong>g some code to be executed at aparticular time, when you may not be able to (or may not want to)change the code that is runn<strong>in</strong>g at that po<strong>in</strong>t. If I want someth<strong>in</strong>g tohappen when a button is clicked, I don’t want to have to change thecode of the button—I just want to tell the button to call one of my methodsthat will take the appropriate action. It’s a matter of add<strong>in</strong>g a level of<strong>in</strong>direction—as so much of object-oriented programm<strong>in</strong>g is. As we’ve seen, this addscomplexity (look at how many l<strong>in</strong>es of code it took to produce so little output!) butalso flexibility.Now that we understand a bit more about simple delegates, we’ll take a brief look atcomb<strong>in</strong><strong>in</strong>g delegates together to execute a whole bunch of actions <strong>in</strong>stead of just one.Reasons fordelegatesexist<strong>in</strong>g2.1.2 Comb<strong>in</strong><strong>in</strong>g and remov<strong>in</strong>g delegatesSo far, all the delegate <strong>in</strong>stances we’ve looked at have had a s<strong>in</strong>gle action. The truth isa little bit more complicated: a delegate <strong>in</strong>stance actually has a list of actions associatedwith it. This is called the <strong>in</strong>vocation list of the delegate <strong>in</strong>stance. The static Comb<strong>in</strong>e andRemove methods of the System.Delegate type are responsible for creat<strong>in</strong>g new delegate<strong>in</strong>stances by respectively splic<strong>in</strong>g together the <strong>in</strong>vocation lists of two delegate<strong>in</strong>stances or remov<strong>in</strong>g the <strong>in</strong>vocation list of one delegate <strong>in</strong>stance from another.Before we look at the details, it’s important to note that delegate <strong>in</strong>stances areimmutable. Once you’ve created a delegate <strong>in</strong>stance, noth<strong>in</strong>g about it can be changed.This makes it safe to pass around delegate <strong>in</strong>stances and comb<strong>in</strong>e them with othersLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!