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.

<strong>C#</strong> 2 and 3: new features on a solid base55fixes this with anonymous methods, and <strong>in</strong>troduces a simpler syntax for the cases whereyou still want to use a normal method to provide the action for the delegate. You can alsocreate delegate <strong>in</strong>stances us<strong>in</strong>g methods with compatible signatures—the method signatureno longer has to be exactly the same as the delegate’s declaration.List<strong>in</strong>g 2.4 demonstrates all these improvements.List<strong>in</strong>g 2.4 Improvements <strong>in</strong> delegate <strong>in</strong>stantiation brought <strong>in</strong> by <strong>C#</strong> 2static void HandleDemoEvent(object sender, EventArgs e){Console.WriteL<strong>in</strong>e ("Handled by HandleDemoEvent");}...EventHandler handler;handler = new EventHandler(HandleDemoEvent);BSpecifies delegatetype and methodhandler(null, EventArgs.Empty);handler = HandleDemoEvent;CImplicitly convertsto delegate <strong>in</strong>stancehandler(null, EventArgs.Empty);handler = delegate(object sender, EventArgs e){ Console.WriteL<strong>in</strong>e ("Handled anonymously"); };handler(null, EventArgs.Empty);handler = delegate{ Console.WriteL<strong>in</strong>e ("Handled anonymously aga<strong>in</strong>"); };handler(null, EventArgs.Empty);MouseEventHandler mouseHandler = HandleDemoEvent;mouseHandler(null, new MouseEventArgs(MouseButtons.None,0, 0, 0, 0));Specifies actionwith anonymousmethodThe first part of the ma<strong>in</strong> code B is just <strong>C#</strong> 1 code, kept for comparison. The rema<strong>in</strong><strong>in</strong>gdelegates all use new features of <strong>C#</strong> 2. The conversion <strong>in</strong>volved C makes eventsubscription code read a lot more pleasantly—l<strong>in</strong>es such as saveButton.Click +=SaveDocument; are very straightforward, with no extra fluff to distract the eye. Theanonymous method syntax D is a little cumbersome, but does allow the action tobe very clear at the po<strong>in</strong>t of creation, rather than be<strong>in</strong>g another method to look atbefore you understand what’s go<strong>in</strong>g on. The shortcut used E is another example ofanonymous method syntax, but this form can only be used when you don’t need theparameters. Anonymous methods have other powerful features as well, but we’ll seethose later.The f<strong>in</strong>al delegate <strong>in</strong>stance created F is an <strong>in</strong>stance of MouseEventHandler ratherthan just EventHandler—but the HandleDemoEvent method can still be used due tocontravariance, which specifies parameter compatibility. Covariance specifies returntype compatibility. We’ll be look<strong>in</strong>g at both of these <strong>in</strong> more detail <strong>in</strong> chapter 5. Eventhandlers are probably the biggest beneficiaries of this, as suddenly the Microsoftguidel<strong>in</strong>e to make all delegate types used <strong>in</strong> events follow the same convention makesDEUsesanonymousmethodshortcutFUsesdelegatecontravarianceLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!