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.

34 CHAPTER 2 Core foundations: build<strong>in</strong>g on <strong>C#</strong> 1your last will and testament. It is a set of <strong>in</strong>structions—“pay the bills, make a donationto charity, leave the rest of my estate to the cat,” for <strong>in</strong>stance. You write it before yourdeath, and leave it <strong>in</strong> an appropriately safe place. After your death, your attorney will(you hope!) act on those <strong>in</strong>structions.A delegate <strong>in</strong> <strong>C#</strong> acts like your will does <strong>in</strong> the real world—as a sequence of actionsto be executed at the appropriate time. Delegates are typically used when the code thatwants to execute the actions doesn’t know the details of what that action should be. For<strong>in</strong>stance, the only reason that the Thread class knows what to run <strong>in</strong> a new thread whenyou start it is because you provide it with a ThreadStart delegate <strong>in</strong>stance.We’ll start our tour of delegates with the four absolute basics, without which noneof the rest would make sense.2.1.1 A recipe for simple delegatesIn order for delegates to do anyth<strong>in</strong>g, four th<strong>in</strong>gs need to happen:■ The delegate type needs to be declared.■ There must be a method conta<strong>in</strong><strong>in</strong>g the code to execute.■ A delegate <strong>in</strong>stance must be created.■ The delegate <strong>in</strong>stance must be <strong>in</strong>voked.Let’s take each of the steps of this recipe <strong>in</strong> turn.DECLARING THE DELEGATE TYPEA delegate type is effectively just a list of parameter types and a return type. It specifieswhat k<strong>in</strong>d of action can be represented by <strong>in</strong>stances of the type. For <strong>in</strong>stance, considera delegate type declared like this:delegate void Str<strong>in</strong>gProcessor (str<strong>in</strong>g <strong>in</strong>put);The code says that if we want to create an <strong>in</strong>stance of Str<strong>in</strong>gProcessor, we’re go<strong>in</strong>g toneed a method with one parameter (a str<strong>in</strong>g) and a void return type (the methoddoesn’t return anyth<strong>in</strong>g). It’s important to understand that Str<strong>in</strong>gProcessor really isa type. It has methods, you can create <strong>in</strong>stances of it, pass around references to <strong>in</strong>stances,the whole works. There are obviously a few “special features,” but if you’re ever stuckwonder<strong>in</strong>g what will happen <strong>in</strong> a particular situation, first th<strong>in</strong>k about what would happenif you were just us<strong>in</strong>g a “normal” reference type.NOTESource of confusion: the ambiguous term “delegate”—Delegates are often misunderstoodbecause the word “delegate” is used to describe both a “delegatetype” and a “delegate <strong>in</strong>stance.” The dist<strong>in</strong>ction between these two isexactly the same as the one that exists between any other type and<strong>in</strong>stances of that type—the str<strong>in</strong>g type itself is different from a particularsequence of characters, for example. I’ve used the terms “delegate type”and “delegate <strong>in</strong>stance” throughout this chapter to try to keep it clearexactly what I’m talk<strong>in</strong>g about at any po<strong>in</strong>t.We’ll use the Str<strong>in</strong>gProcessor delegate type when we consider the next <strong>in</strong>gredient.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!