C # delegates and events

Source: Internet
Author: User

A delegate is a chain of methods that allows an object to execute the method on the chain in turn. Can simplify code and improve efficiency. Statement
public delegate int delegate (int i);//Declaration Delegate

Matching rules for delegate registration methods: The method amount return type must be the same as the return type of the delegate. The parameter of the method must be the same as the delegate's method parameter, and the parameter name can be different.
public void F1 (int i,int j);//nopublic int F2 (int i);//nopublic int F3 (int m,int n);//yes

Instantiating a delegate delegate is a class, so you need to instantiate it.
namespace Test{delegate int delegate (int i), class program{public int F1 (int i) {return i;} static void Main (string[] args) {program P=new program ();D elegate d1=new Delegate (P.F1);//Create Instance}}}



A delegate's method list binds multiple methods to the same delegate variable. , you can call all bound methods at one time when the delegate variable is called.
namespace Test{delegate int delegate (int i), class Program<pre Name= "code" class= "CSharp" >namespace test{public delegate int delegate (int i);p ublic class program{public static int F1 (int i) {return i;} public int F2 (int j) {return J;} static void Main (string[] args) {program P=new program ();D elegate d1=new Delegate (p.f1);D elegate d2=new Delegate (P.F2);D Elegate d3=d1+d2;//to delegate registration method int I3=D3 (10);}}

Calling a delegate delegate is a method chain that invokes a delegate that invokes all the methods contained by the delegate instance.
namespace Test{public delegate void delegate (int i);p ublic class program{public static void F1 (int i) {Console.WriteLine (i . ToString ());} public static void F2 (int i) {cosnole.writeline (i.tostring ());} static void Main (string[[] args) {Delegate d1=new Delegate (program.f1);D elegate d2=new Delegate (PROGRAM.F2);D elegate D3 =D1+D2;D1 (10);//Call D1 Instance D2 (200);//Call D2 instance D3 (201);//Call D3 instance}}}

An event event is a special delegate that declares an event
public delegate void EventHandler (Object Sender,eventargs e);

Declaring the event itself
Public event EventHandler print;//Declaration event Print

Registering Events & removing events
namespace Test{public delegate void EventHandler (Object Sender,eventargs E) class Program{public event EventHandler Print ;p ublic void F1 (Object Sender,eventargs e) {Console.WriteLine ("F1");} public void F2 (Object Sender,eventargs e) {Console.WriteLine ("F2");} static void Main (string[] args) {program P=new program ();p. Print+=new EventHandler (P.F1);//Register a method p for the event. Print+=new EventHandler (P.F2); if (p.print!=null) {p.print (null,null);} Console.WriteLine ();p. Print-=new EventHandler (P.F1);//Remove the method from the event f1;if (p.print!=null) {p.print (null,null);} Console.ReadLine ();}}}

Invoke Event
namespace Test{public delegate void EventHandler (Object Sender,eventargs e); class Program{public Event EventHandler print;public void F1 (Object Sender,eventargs e) {Console.WriteLine ("F1");} static void Main (string[] args) {program P=new program ();p. Print+=new EventHandler (P.F1); if (p.print!=null) {P.print ( Null,null);//Call Event}console.readline ();}}}



C # delegates and events

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.