7. Simple application of object-oriented and WinForm (entrustment)

Source: Internet
Author: User

Commissioned

1. Concept:

A delegate can be thought of as a "container" of a function, and it can be used as a function after "loading" a specific function. A delegate variable can be thought of as a type-safe function pointer that can receive only the address of a function that meets its requirements.

2, definition method: Delegate keyword. Cases:

     Public Delegate int Mydele (intint b);

3, using the delegate call function

1) Define a class first and write a few methods:

    class Class1    {        publicint jiafa (intint  b)        {            return A + b;        }          Public int JIANFA (intint  b)        {            return A- b;        }
}

2) Call the function through the delegate:

//Define a variable of an existing delegate type New //Assign values to the above variables Console.WriteLine (M (1,2));  The result is 3new Class1 (). JIANFA; Console.WriteLine (M (1,2)); //result is-1

4. Note: A variable of a delegate type can refer to any function that satisfies its requirements.

5, the extension of the delegation:

1) Special delegate generic set, anonymous delegate

func<stringstringnew Class1 (). returnstring; // input string, output string Console.WriteLine (MyFunc (" Zhang San "));

2) Direct Write method

func<stringstringdelegate(string  s) {      return]  world! " ;            }; Console.WriteLine (Newfunc (" John Doe "));

3) Lambda method simple version 1, replace delegate with s=> (string s)

func<stringstring> newfunc1 = s=> {          return'world! "

4) lambda method simple version 2, two parameters

func<stringstring,string> Newfunc2 = (s1,s2) + =         { return " world! "+s2;             

5) Lambda method simple version 2, remove curly braces, direct write method

func<stringstring,string> newfunc3 = (S1, s2) =>s1+"world! "+s2;             
Console.ReadLine ();

7. Simple application of object-oriented and WinForm (entrustment)

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.