C # Delegate Learning

Source: Internet
Author: User

.. > Preface

  Inability to spit groove today's weather, especially cold, drink a cup of tea to continue to write something!

Commissioned

A delegate is a reference type, similar to a pointer type in C, but this "pointer" must point to a function of some kind.

Objects are also reference types, note the difference between reference types and value types. For example: Write down the results of the following programs.

1 classA2     {3          Public intx;4     }5     class Program6     {7         Static voidMain (string[] args)8         {9A A =NewA ();Tena.x =5; OneA B =A; Ab.x =6; -Console.WriteLine (a.x);//6 -Console.WriteLine (b.x);//6 theA C =NewA (); -c =A; -c.x =7; -Console.WriteLine (a.x);//7 +Console.WriteLine (c.x);//7 - Console.read (); +         } A}

Defines the delegate with the keyword delegate, in the form:

Access modifier delegate return value type method name (formal parameter list);

For example: publicdelegate int mydelegate (int x,int y);

Note the defined delegate is just a type that creates an instance of this type to complete the operation on the delegate. When you create an instance of a delegate, you must first define the function, or instantiate an object that encapsulates the method of the function or object into a variable of the newly created delegate type.

The operations of a delegate are:

    =, the other function is encapsulated in the delegate.

+, add another function to the delegate

-, remove another function from the delegate

    For example, the following programs: 

One:

1          Public Delegate intMyDelegate (intXinty);2         Static intFintAintb)3{returnA +b;}4         Static intffintXinty)5{returnx > y?x:y;}6         Static voidMain (string[] args)7         {8MyDelegate a =Newmydelegate (f);9Console. WriteLine (A (3,5));//8TenMyDelegate B =Newmydelegate (FF); OneConsole.WriteLine (b (3,5));//5 AMyDelegate c=a+b;//swap A with B to see what the results are?  -Console. WriteLine (C (3,5));//5, here to add, C (3,5) is the return value of two methods, because the return value of the following method overrides the return value of the previous method, so only print out the return value of the following method, students do not confuse it here.  -c = c-b;//Change B to a to see what happens?  theConsole.WriteLine (C (3,5));//8 - Console.read (); -}

Two:

1         Public Delegate voidMyDelegate (intXinty);2         Static voidFunintAintb)3{Console.WriteLine (A +b); }4         Static voidFUN1 (intXinty)5{Console.WriteLine (x > y?)x:y); }6         Static voidMain (string[] args)7         {8MyDelegate a =Newmydelegate (fun);9A3,5);//8TenMyDelegate B =Newmydelegate (FUN1); Oneb3,5);//5 AMyDelegate C = a + B;//swap A with B to see what the results are?  -C3,5);//8, 5, every method here has an output (see the method above??). ), so there are two results, uh.  -c = c-b;//Change B to a to see what happens?  theC3,5);//8 - Console.read (); -}

    See here is not feel entrusted actually also not what, welcome everyone's opinion OH

  

C # Delegate Learning

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.