Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Page 2 - A Quick Description of C# Delegates
A Quick Description of C# Delegates
By Jeff Cogswell

Rate This Article: Add This Article To:

A Quick Description of C# Delegates - ' The Quick Answer'
( Page 2 of 2 )

Third Example: The Quick Answer

A delegate is an object that holds a reference to a method, and, optionally, a specific instance.

ADVERTISEMENT

An event handler is an example of a delegate being used. When you create code for a button's click handler, the form saves a delegate containing your handler and the form instance. When the button gets clicked, the form calls your handler by calling the delegate. When your handler gets called, the method is associated with the specific form instance.

Delegates are of specific types. When you declare a delegate type, you specify the footprint for methods that the delegate can store. For example, if you declare a delegate type like so:

public delegate string mydel(int number);

then you are saying that delegate objects of this type can hold a pointer to a method that takes an integer as a parameter and returns a string.

This only declares the type, however. To create the delegate itself, you create it as you would any other object, like so:

mydel del1 = obj1.myfunc;

where myfunc is the name of a method that takes an integer as a parameter and returns a string. The following is a complete example that demonstrates this.

using System;
using System.Collections.Generic;
using System.Text;

public class MyClass
{
    int x;
    public MyClass(int x1)
    {
        x = x1;
    }
    public string myfunc(int n)
    {
        return (n * x).ToString();
    }
}

public delegate string mydel(int number);

class Program
{
    static void Main(string[] args)
    {
        MyClass obj1 = new MyClass(10);
        MyClass obj2 = new MyClass(20);

        mydel del1 = obj1.myfunc;
        mydel del2 = obj2.myfunc;
        Console.WriteLine(del1(50));
        Console.WriteLine(del2(20));
    }
}

Delegates can be chained together so that they called in sequence. You use the += syntax like so:

mydel del1 = obj1.myfunc;
del1 += obj2.myfunc;

When you invoke the del1 delegate, first obj1.myfunc will get called, and then obj2.myfunc will get called. (In this example, the myfunc method doesn't do anything besides return a string, so calling the methods in sequence doesn't make much sense. More likely you would chain together methods that actually do some work.)

Now here's a sample that demonstrates a delegate storing a static method:

using System;
using System.Collections.Generic;
using System.Text;

public class MyClass
{
    public static int staticFunc(int n)
    {
        return n * 2;
    }
}

public delegate int anotherdel(int number);

class Program
{
    static void Main(string[] args)
    {
        anotherdel del1 = MyClass.staticFunc;
        Console.WriteLine(del1(50));
    }
}

You can see in this code that the delegate anotherdel is declared, and then an instance of the delegate, del1, is created. Since staticFunc is a static method, I simply access it through MyClass directly. Finally, notice also that when I declared the delegate type, I don't specify whether it holds static or member methods; it can, in fact, hold either.

Conclusion

One thing I didn't mention is that when you create a delegate, you are creating an instance of the .NET class called Delegate. Now that you have a feel for what exactly delegates are, take a look at the MSDN help entry for Delegate, as well as the entry on MulticastDelegate (which supports chaining). And as always, have fun!



 
 
>>> More Languages Articles          >>> More By Jeff Cogswell
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.