What is a delegate
First you need to know what a delegate is, and in the most understandable words, you can think of a delegate as a thing to execute a method (function).
How to use Delegates
When using a delegate, you can treat it as if it were a class. It is declared first, then instantiated. Just a little different, a class is called an object or an instance after it is instantiated, but the delegate is still called a delegate after it is instantiated.
-----------------------------------------------
Define a delegate:
A delegate's health: it can be born anywhere within the namespace. Keyword used: delegate
public delegate void Greethandler ();//define Delegate
Class Program
{
static void Main (string[] args)
{
Create a delegate method (method one)
Greethandler Greethandler;
Greethandler = chinesegreet;//Assignment (the first method can only use an assignment character)
Greethandler + = englishgreet;//Binding method
Greethandler-= chinesegreet;//unbind
Greethandler ();//Use Delegate
Create a delegate method (method two)
Greethandler Greethandler = new Greethandler (chinesegreet);
Greethandler + = Englishgreet;
Greethandler ();
}
public static void Chinesegreet ()
{
Console.WriteLine ("Good Morning!") ");
}
public static void Englishgreet ()
{
Console.WriteLine ("Good morning!");
}
}
---------------------------------
Delegate with parameter type
Mr. Ming a class of
Class arithmetic
{
public int Add (int num1,int num2)
{
return NUM1 + num2;
}
public int Reduce (int num1, int num2)
{
return num1-num2;
}
}
If the argument is a reference type: Add a ref keyword to the front of the argument
public delegate int Arithhandler (int num1, int num2);
Class Program
{
static void Main (string[] args)
{
Arithmetic arithmetic = new arithmetic ();
Arithhandler Arithhandler = new Arithhandler (arithmetic. ADD);
Arithhandler + = arithmetic. Reduce;
int result = Arithhandler (5, 3);//result receives the result of the last method execution
Console.WriteLine ("Result is:" + result);
}
}
------------------------
Delegates can also call anonymous methods many times for methods that are called only once:
public delegate void Hellohandler ();
public delegate int Calhandler (int num1,int num2);
Class Program
{
static void Main (string[] args)
{
Assigning an anonymous method to a delegate (anonymous method without parameters and no return value)
Hellohandler Hellohandler = Delegate ()
{
Console.WriteLine ("Hello! ");
};
Hellohandler ();
Anonymous method with a parameter return value
Calhandler Calhandler = delegate (int a, int b)
{
int num = a + b;
return num;
};
int result = Calhandler (5,3);
Console.WriteLine (result);
}
}
--------------------------------------------
Event events are a special kind of delegate
/********* Event *************/
public delegate void Greethandler ();//define Delegate
Class Program
{
public static event Greethandler myevent;//Declaration event (publish event)
static void Main (string[] args)
{
MyEvent + = new Greethandler (chinesegreet);//Subscribe to Events
MyEvent + = new Greethandler (englishgreet);
MyEvent ();//Trigger Event
}
public static void Chinesegreet ()
{
Console.WriteLine ("Good Morning!") ");
}
public static void Englishgreet ()
{
Console.WriteLine ("Good morning!");
}
}
The birth and use of the entrustment