. NET delegate

Source: Internet
Author: User

    Delegate Type Definition
 





  • Delegated instance
    When to use delegation instead of using interfaces
  • Use delegation in the following cases
  • Use the interface in the following cases
  • Delegate Declaration
   ProcessBookDelegate(Book book);
  • Instantiate Delegation
  • Call delegate
  • Multi-channel broadcast Delegation
 
  Del( s); TestClass{      Hello( s)    {        System.Console.WriteLine(, s);    }      Goodbye( s)    {        System.Console.WriteLine(, s);    }      Main()    {        Del a, b, c, d;        a = Hello;        b = Goodbye;        c = a + b;        d = c - a;        System.Console.WriteLine();        a();        System.Console.WriteLine();        b();        System.Console.WriteLine();        c();        System.Console.WriteLine();        d();    }}
    Anonymous Method
 
(System.Object o, System.EventArgs e)                   { System.Windows.Forms.MessageBox.Show(); };
  Del( x);Del d = ( k) {  };
 StartThread(){    System.Threading.Thread t1 =  System.Threading.Thread      (()            {                System.Console.Write();                System.Console.WriteLine();            });    t1.Start();}

  • Co-Variation and inverter in Delegation
 Mammals{} Dogs : Mammals{} Program{      Mammals HandlerMethod();      Mammals FirstHandler(){ ;}      Dogs SecondHandler(){ ;}      Main()    {        HandlerMethod handler1 = FirstHandler;        HandlerMethod handler2 = SecondHandler;    }}
 Form1(){    InitializeComponent();    lastActivity =  System.DateTime();    .textBox1.KeyDown += .MultiHandler;     .button1.MouseClick += .MultiHandler; }  MultiHandler( sender, System.EventArgs e){    lastActivity = System.DateTime.Now;}
  • Generic Delegation
   Del<T>(T item);   Notify( i) { }Del<> m1 =  Del<>(Notify);
  • Method group Conversion
> m2 = Notify;
  • Generic class internal definition delegate use of generic class type parameters
 Stack<T>{    T[] items;     index;       StackDelegate(T[] items);}
  • The code that references the delegate must specify the type variable containing the class
   DoWork([] items) { }   TestStack(){    Stack<> s =  Stack<>();    Stack<>.StackDelegate d = DoWork;}
  • Generic delegation replaces traditional binning Delegation
  StackEventHandler<T, U>(T sender, U eventArgs); Stack<T>{      StackEventArgs : System.EventArgs { }      StackEventHandler<Stack<T>, StackEventArgs> stackEvent;       OnStackChanged(StackEventArgs a)    {        stackEvent(, a);    }} SampleClass{      HandleStackChange<T>(Stack<T> stack, Stack<T>.StackEventArgs args) { }}   Test(){    Stack<> s =  Stack<>();    SampleClass o =  SampleClass();    s.stackEvent += o.HandleStackChange;}

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.