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.

Say<strong>in</strong>g goodbye to awkward delegate syntax139Likewise it’s undeniably ugly to write methods that are so simple that their implementationis shorter than their signature, solely because delegates need to have codeto execute and that code has to be <strong>in</strong> the form of a method. It adds an extra layer of<strong>in</strong>direction between the code creat<strong>in</strong>g the delegate <strong>in</strong>stance and the code that shouldexecute when the delegate <strong>in</strong>stance is <strong>in</strong>voked. Often extra layers of <strong>in</strong>direction arewelcome—and of course that option hasn’t been removed <strong>in</strong> <strong>C#</strong> 2—but at the sametime it often makes the code harder to read, and pollutes the class with a bunch ofmethods that are only used for delegates.Unsurpris<strong>in</strong>gly, all of these are improved greatly <strong>in</strong> <strong>C#</strong> 2. The syntax can still occasionallybe wordier than we might like (which is where lambda expressions come <strong>in</strong>toplay <strong>in</strong> <strong>C#</strong> 3), but the difference is significant. To illustrate the pa<strong>in</strong>, we’ll start withsome code <strong>in</strong> <strong>C#</strong> 1 and improve it <strong>in</strong> the next couple of sections. List<strong>in</strong>g 5.1 builds a(very) simple form with a button and subscribes to three of the button’s events.List<strong>in</strong>g 5.1Subscrib<strong>in</strong>g to three of a button's eventsstatic void LogPla<strong>in</strong>Event(object sender, EventArgs e){Console.WriteL<strong>in</strong>e ("LogPla<strong>in</strong>");}static void LogKeyEvent(object sender, KeyPressEventArgs e){Console.WriteL<strong>in</strong>e ("LogKey");}static void LogMouseEvent(object sender, MouseEventArgs e){Console.WriteL<strong>in</strong>e ("LogMouse");}...Button button = new Button();button.Text = "Click me";button.Click += new EventHandler(LogPla<strong>in</strong>Event);button.KeyPress += new KeyPressEventHandler(LogKeyEvent);button.MouseClick += new MouseEventHandler(LogMouseEvent);Form form = new Form();form.AutoSize=true;form.Controls.Add(button);Application.Run(form);The output l<strong>in</strong>es <strong>in</strong> the three event handl<strong>in</strong>g methods are there to prove that the codeis work<strong>in</strong>g: if you press the spacebar with the button highlighted, you’ll see that theClick and KeyPress events are both raised; press<strong>in</strong>g Enter just raises the Click event;click<strong>in</strong>g on the button raises the Click and MouseClick events. In the follow<strong>in</strong>g sectionswe’ll improve this code us<strong>in</strong>g some of the <strong>C#</strong> 2 features.Let’s start by ask<strong>in</strong>g the compiler to make a pretty obvious deduction—which delegatetype we want to use when subscrib<strong>in</strong>g to an event.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!