Delegate Simple example

Source: Internet
Author: User

Just beginning to learn C #, the delegates and events feel too difficult to understand, recently saw a lot of online writing about this example, here I mark, according to their own understanding of the note, the right to take a note

One, commissioned

First of all, we define a delegate, the name is taken, we can understand it.

 Public Delegate int mydelegate (int x);

For this delegate, let's write a method that returns the same value as the parameter.

         Public Static int GetXXX (int  x) {            2);             return 2 ;        }

The method is simple, multiply the value by 2 and return back to see how the delegate is called.

MyDelegate MD = GetXXX;             int x = MD (3);

Through the delegate, we can call to getxxx this method, some people say, the direct call to GetXXX (3) method does not become, where is the role of the delegate? Let's do one more way.

         Public Static int GETSSS (int  x) {            one);             return  One ;        }

When we call, in addition to calling the previous getxxx, you can also call continue call Getsss This method, this is called multicast delegation.

            MyDelegate MD = GetXXX;             + = getsss            ; int x = MD (3);

In this case, you can use + = to invoke multiple methods, as well as to remove a method with-=.

Another use of a delegate is to pass a delegate as a parameter

        // Create a method to delegate as a parameter         Public Static int Calculatebyd (int  x, MyDelegate d) {            return  D (x);        }             // Pass the method name getxxx as a parameter, and then pass the GETSSS to implement this plug-in programming            Calculatebyd (2, GetXXX);            Calculatebyd (2, getsss);

There is also a generic delegate that, if you know about generics, is not difficult to understand.

// generic delegate declaration         Public Delegate T mydelegate2<t>(t x);         // generic delegate Events         Public Static T calculate2<t> (t X, mydelegate2<t> c) {            return  C (x);        }             // Invoke Event            mydelegate2<int> MD2 = GetXXX;            Calculate2 (8, MD2);

There is also an anonymous delegate, you need to know

// do not specify a specific method invocation delegate            int y = Calculate2 (89);
Func and Action Delegate

With generic delegates, there is a generic delegate, Func, and Action that can be applied to any return type and any parameter (type and reasonable number). As shown below (the following in indicates the parameter, out indicates the return result):

Delegate TResult func <out tresult> ();d elegate TResult func <in T, out tresult> (T Arg);d elegate TResult func <in T1, in T2, out tresult> (T1 arg1, T2 arg2); .... Until t16delegate void action ();d elegate void action <in t> (T arg);d elegate void action <in T1, in t2> (T1 arg 1, T2 arg2);.. Until T16.

With this generic delegate, we can delete the calculator generic delegate above, and the example will be more concise:

public static void Calculate<t> (t[] values, func<t,t> c) {for    (int i = 0; i < values. Length; i++)        Values[i] = C (Values[i]);}

Func and Action delegates, in addition to the ref parameter and out parameters, can be applied to virtually any generic delegate scenario, which is very useful.

Delegate Simple example

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.