C # commissioned the basic series was originally published in February 2011 in my Sina blog, now it will be like this blog.
the delegate and its delegate methods must have the same signature. Same signature: 1. Same parameter type 2. Same number of parameters 3. Return values consistent
Example One
Class program
{public
delegate int Mathmethod (int x, int y);
public int Add (int a, int b)
{return
a + b;
}
static void Main (string[] args)
{
mathmethod mm;
Program P = new program ();
MM = p.add;//is equivalent to the container Console.WriteLine of a Method
("evaluates to {0}", mm (7,6));
Console.ReadLine ();
}
Case II
Class Program {public delegate double Mathmethod (double x, double y);
Double Add (double A, double b) {return a + B;
Double subtract (double A, double b) {return a + B;
Double Multiply (double A, double b) {return a * b;
Double Divide (double A, double) {return A/b;
} void Docalculate (Mathmethod mm) {Console.WriteLine ("Please enter first number");
Double x = convert.todouble (Console.ReadLine ());
Console.WriteLine ("Please enter a second number");
Double y = convert.todouble (Console.ReadLine ());
Console.WriteLine ("Result {0}", mm (x, y));
Console.ReadLine ();
} static void Main (string[] args) {mathmethod mm;
Program P = new program ();
MM = P.divide;
P.docalculate (mm); }
}
This article refers to the contents of the agent from Jin Xu-liang's ". NET 4.0 object-oriented Programming".