posts - 28 , comments - 23 , trackbacks - 0

My Links



Tag Cloud


The Road To LINQ-dom Part 2–Delegates

In my previous post we started a discussion about concepts that are critical to really understanding LINQ.  The next couple of posts will build on the same theme by introducing three very closely related (and somewhat evolutionary) pieces of the C# language – delegates, anonymous functions, and lambdas.  My original intent had been to introduce them together in single post, but that would be a rather long post.

Delegates are a language feature that have been in C# since version 1 of the language and act similarly to function pointers in other language.  Most simply put, they allow you to have a variable that itself is a function.  This allows scenarios where you can have code that only knows the “shape” of the method its calling (the inputs and outputs) and is somewhat shielded from implementation details.  The most common example of delegates in the .Net world has to be events.  When you write the Click handler for a button in a C# WinForms application, you are actually writing a delegate.  At runtime, the code in Button checks to see if its delegate has been set and then calls your code having no idea what your code will do – only that it is the required shape.

In order to use delegates, the first thing you need to do is to declare the delegate.  The declaration specifies a name for the delegate, the return value, and any parameters:

private delegate void PrintSomething(string message);
after declaring the delegate, you’re able to use it in your code as demonstrated in this small console application:
   1:      class Program
   2:      {
   3:          private delegate void PrintSomething(string message);
   5:          static void Main(string[] args)
   6:          {
   7:              PrintSomething func;
   8:              func = PrintSomethingToConsole;
   9:              func("My Message");
  11:              Console.ReadKey();
  12:          }
  14:          static void PrintSomethingToConsole(string message)
  15:          {
  16:              Console.WriteLine(message);
  17:          }
  18:      }

You can really start to see possibilities when you think about using delegates as arguments to methods to make the method more dynamic, but there’s one more really cool thing about delegates that I want to share before we’re done with this post – they can be combined.  What this means is that you can use C# + and += operators to assign multiple methods (all having a matching shape) to the delegate variable and it will invoke each of the methods whenever it is called.  Here’s an example of using a single delegate variable to invoke multiple methods (look at output both in console and debug output windows):

   1:      class Program
   2:      {
   3:          private delegate void PrintSomething(string message);
   5:          static void Main(string[] args)
   6:          {
   7:              PrintSomething func;
   8:              func = PrintSomethingToConsole;
   9:              func += PrintSomethingToDebug;
  10:              func("My Message");
  12:              Console.ReadKey();
  13:          }
  15:          static void PrintSomethingToConsole(string message)
  16:          {
  17:              Console.WriteLine(message);
  18:          }
  20:          static void PrintSomethingToDebug(string message)
  21:          {
  22:              Debug.WriteLine(message);
  23:          }
  24:      }

This post has only scratched the surface of delegates so that I could avoid giving too much information to be useful for the target audience, so don’t stop here.  Play around with the example, add some of your own, and use the power of interweb searching to dig deeper.

Print | posted on Thursday, December 8, 2011 8:27 AM |



# re: The Road To LINQ-dom Part 2–Delegates

Good post - once you start using lambdas, it's easy to forget about delegates (until needed of course), especially once you become familiar with LINQ extensions. LINQ to everything... :)
12/8/2011 2:01 PM | Brandon

# re: The Road To LINQ-dom Part 2–Delegates

Thanks Brandon. Will hit lambdas in a couple of posts.
12/8/2011 2:10 PM | Kyle Burns
Post A Comment

Powered by: