The birth and use of the entrustment

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.