Letter of authorization

Source: Internet
Author: User

Letter of authorization

Delegate: similar to enumeration, it can be defined in the class or namespace.

Declaration: public delegate int Entrust (int x, int y); note: the parameter passed in when the delegated instance is created is the method name.

Class MyMath {public int add (int x, int y) {return x + y;} public int sub (int x, int y) {return x-y ;}}Preparation

Use: Entrust e = new Entrust (MyMath. add); Note: static methods can be used like this; otherwise, create an object of MyMath first.

Multicast delegation: e + = MyMath. sub; Note: it is best not to use multicast Delegation for methods with return values.

Anonymous delegate: e + = delegate (int x, int y) {return x * y ;};

Lambda: e + = (x, y) => x/y; Note: build tomorrow

Use: Console. WriteLine (e (8, 2 ));

Pre-defined commonGeneric Delegation:

Func delegate: there are multiple overload types: delegate TResult Func <T1, T2, T3, T4, TResult> -- Func <string, int> f = delegate (string x) {return 1 ;}; Note: The last parameter of the generic type is the return value type.

Action delegate: no return value relative to Func delegate

Predicate delegation: determines whether a parameter meets a certain standard. Only one parameter can be accepted. The parameter type is specified in <> and the return value is bool. Predicate <string> p = delegate (string x) {return true ;};

Comparison delegate: Compares two elements. The two parameters must be of the same type. The parameter type is specified in <> and the return value is int, which is generally used for sorting. Comparison <string> c = delegate (string x, string y) {return 1 ;};

 

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.