Delegates in C# - an introduction(转:英文)

发表于:2007-06-30来源:作者:点击数: 标签:
Introduction I assume that most people who want to learn C# are C/C++ programmers. Thus I believe that they will be looking for features in C# which are analogous to some C/C++ feature they were quite fond of. And one of my favorite feature
Introduction
I assume that most people who want to learn C# are C/C++ programmers. Thus I believe that they will be looking for features in C# which are analogous to some C/C++ feature they were quite fond of. And one of my favorite features about good old C was function pointers. Those of you who haven@#t used function pointers missed out on the fun. Well C# does have something that can be used where we used to use function pointers. Actually they do a lot more than function pointers used to do. They are called delegates. As is usual with me, I@#ll try and demonstrate the use of delegates through commented, sample programs that are small, simple and hopefully easy to understand.

Program 1
In this program we@#ll see how delegates are used to encapsulate a reference to a method in a delegate object. As you can see we can declare delegates in a namespace and thus delegates are shareable among classes. You can also see that we can associate a delegate variable with both static and instance member functions.

using System;
//declare our delegate type
public delegate void dgate();
class nish
{
    public static void Main()
    {
        test t1 = new test();
        //create a new delegate object and associate it
        //with the function abc in class test        
        dgate d1 = new dgate(t1.abc);
        d1(); //call the delegate
        
        //now associate the delegate object with
        //the function xyz in class test        
        d1 = new dgate(t1.xyz);
        d1(); //call the delegate
        
        //here we associate the delegate with the static
        //function HelloWorld from this same class
        d1 = new dgate(HelloWorld);
        d1(); //call the delegate
    }
    public static void HelloWorld()
    {
        Console.WriteLine("Hello World");
    }    
}

class test
{
    public void abc()
    {
        Console.WriteLine("This is test.abc()");
    }
    public void xyz()
    {
        Console.WriteLine("This is test.xyz()");
    }
}

OUTPUT

F:c#delegates>deleg01
This is test.abc()
This is test.xyz()
Hello World

F:c#delegates>
Program 2
This program will demonstrate how you can compose a delegate from two other delegates using the + operator and also how you can remove a component delegate from a composed delegate using the - operator. The output of the program should make things quite clear. A composed delegate can consist of one or more components of one or more delegates. Thus, for example, you can have a composed delegate with three components, two which are both of the same delegate type.

using System;
public delegate void dgate(string s1);

class nish
{
    public static void Main()
    {
        //declare four dgate delegates
        dgate a1,a2,a3,a4;
        
        a1=new dgate(morning);
        a2=new dgate(night);
        //composing a3 by adding a1 and a2
        a3=a1+a2;
        //removing a1 from the composed delegate
        a4=a3-a1;
        
        Console.WriteLine("calling A1");
        a1("A1");
        Console.WriteLine(" calling A2");
        a2("A2");
        Console.WriteLine(" calling A3");
        a3("A3");
        Console.WriteLine(" calling A4");
        a4("A4");
        
        //You can add the same delegate several times
        //and you can have the same delegate variable as LHS and RHS
        a1=a1+a1+a1;
        Console.WriteLine(" calling A1");
        a1("A1");
    }
    public static void morning(string s1)
    {
        Console.WriteLine("Good Morning "+s1);
    }
    public static void night(string s1)
    {
        Console.WriteLine("Good Night "+s1);
    }    
}

OUTPUT

F:c#delegates>deleg02
calling A1
Good Morning A1

calling A2
Good Night A2

calling A3
Good Morning A3
Good Night A3

calling A4
Good Night A4

calling A1
Good Morning A1
Good Morning A1
Good Morning A1

F:c#delegates>
Program 3
In this program I will show you how you can pass a delegate object to a function. You can see how delegates are useful in splitting up functionality. The Display() function has no idea, what function the delegate passed to it is referencing. Thus we can have separate functional blocks all independent of each other.

using System;
public delegate int dgate(int i);
class nish
{
    public static void Main()
    {
        //associate d1 to static function square
        dgate d1=new dgate(square);
        Display(d1,"square");
        
        //associate d1 to static function cube
        d1=new dgate(cube);
        Display(d1,"cube");
    }
    
    /* this function takes two parameters, a delegate of type dgate and a string */
    public static void Display(dgate d1, string s)
    {
        Console.WriteLine(s+" of 5 is "+d1(5));
    }
    
    public static int square(int y)
    {
        return y*y;
    }
    public static int cube(int y)
    {
        return y*y*y;
    }
}

OUTPUT

F:c#delegates>deleg03
square of 5 is 25
cube of 5 is 125

F:c#delegates>

原文转自:http://www.ltesting.net