Page 1 of 1

C# Delegates A quick introduction to C# Delegates

#1 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 71
  • View blog
  • Posts: 765
  • Joined: 15-April 09

Post icon  Posted 02 October 2009 - 11:10 AM

*
POPULAR

In C and C++ there were function pointers. Delegates are C#'s object oriented and type safe answer to function pointers.

Let's say you have the following two methods:
public void WriteOut()
{
	 Console.WriteLine("WriteOut method called");
}
public void WriteIn()
{
	 Console.WriteLine("WriteIn method called");
}

In your code, you want to call one of these based on the value of the integer selected, you could write it using an if statement like this:
If (selected == 1)
{
	 WriteOut();
}
else if(selected == 2)
{
	 WriteIn();
}
else
{
	 Console.WriteLine("Hunh?");
}

But what happens if you add more methods later? You'd have to add more else statements, eventually resulting in unwieldy code and really long source files, especially if you have hundreds of such methods.

So what's a better option? Use an array of delegates. Before I show you how to do this, let me show you how to declare and use a delegate to begin with.

First we declare the delegate:
delegate void SimpleDelegate();

Now, let's create an instance of the delegate and assigne the WriteOut method:
SimpleDelegate d = new SimpleDelegate(WriteOut);

Now, instead of calling WriteOut directly, we can call the delegate like this:
d();

Yes, that's right, we just called the WriteOut method by calling on the delegate d. But how is this handy? Well, think of it this way, now that we have a reference to a method we can create an array containing the references. This is something we couldn't do with method names. For instance:
delegate void SimpleDelegate();
SimpleDelegate[] void d() = new SimpleDelegate[2]();
d[0]() = SimpleDelegate(WriteIn);
d[1]() = SimpleDelegate(WriteOut);

Now that we've setup our delegate array, the next and final piece of the puzzle is to iterate through the array calling each referenced method.
For (int a = 0; a < 2; a++)
{
   d[a]();
}

That's it, we've just called both functions from within a for loop. Realistically we'd never loop through the entire array calling each referenced method, instead we'd want to call just one method based on the value of an integer, but this example provides you all the necessary knowledge to get it done.

What if we did want to call every method all at once? Well, there's another option with delegates that we haven't yet explored. Let's rewrite our delegate definitions:
delegate void SimpleDelegate();
SimpleDelegate void d() = new SimpleDelegate();
d() = SimpleDelegate(WriteIn);
d() += SimpleDelegate(WriteOut);
d();

In the above code we've declared a single delegate. In the second line we assign the WriteIn method to the delegate, and in the third line we add the WriteOut method to the delegate. When we call the delegate in line four it will call the WriteIn method, then it will call the WriteOut method. We've now called all the delegate's methods without having to use a for loop.

But what if we later want to remove a method from a delegate? Continuing the previous example we can do it like this:
d() -= SimpleDelegate(WriteOut);

Now the WriteOut method is removed from the delegate, but the delegate still contains a reference to the WriteIn method.

There are several rules regarding delegates that you should know of. First, when using a delegates all methods being assigned must have the same parameter list as when you declare the delegate. You can declare a delegate with parameters like:
Delegate int SimpleDelegate(int Number1, int Number2);

All methods being assigned to a delegate must also provide the same type return value as the delegate declared. In the preceeding example, all assigned methods must accept two integer parameters and return one integer value.

If you call upon a delegate that contains references to two or more methods, the values passed into the parameters are counted as local variables for as long as the delegate runs. If the first method modifies the value of a passed parameter, the modified value is what is passed to the second method, and so on and so forth until all referenced methods have ran.

Is This A Good Question/Topic? 12
  • +

Replies To: C# Delegates

#2 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 256
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 13 January 2011 - 02:19 AM

Very, very useful tutorial, Delegates always seemed a distant dream for me in terms of understanding, but you have made it seem so simple!

Thank you very much will +Rep when I get home ^^
Was This Post Helpful? 1
  • +
  • -

#3 JesseLord  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 24-May 10

Posted 15 January 2011 - 11:45 PM

I give Rep. I always read about them, but when I have been using them they were for buttons and radio buttons and things of that nature. But the principle is the same. I gave a rep point for showing me a different way to think about them and streamline them.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1