Differences between func and Resource Sharing

Source: Internet
Author: User

I. [Action <>] specifies the delegates with only input parameters and no return value.

1.1 define a delegate:

For example, the original write delegate:

Public Delegate void mydelegate (string Str); this delegate indicates that it can only delegate methods with only one input value but no return value, such as the following method.

1.2 definition method

Public static void hellowchinese (string strchinese)

{

Console. writeline ("good morning," + strchinese );

}

1.3 call

Mydelegate d = new mydelegate (hellowchinese );

D ("Wang Zong ");

1.4 Use action to delegate this method.

If we use the original method to call the hellowchinese method, it is very troublesome and we have to define a delegate. Do you need to clearly define the delegation?

All right, Microsoft has implemented the authorization of action. We only need to pass the methods that meet its requirements into it ..

 

I still use this method:

Public static void hellowchinese (string strchinese)

{

Console. writeline ("good morning," + strchinese );

 

}

 

When I call this method, I do not need to customize the delegate. In this way, you can:

Action <string> action = hellowchinese;

Action ("Wang Zong ");

 

This saves time and reduces many steps.

 

 

II. Func <> This is the same as the one above. The difference is that there is a returned value !~~~

2.1 previously we defined delegate

Public Delegate string mydelegate (string Str );

2. Definition Method

Public static string hellowenglish (string strenglish)

{

Return "Hellow" + strenglish;

}

2.3 call

Mydelegate d = new mydelegate (hellowenglish );

D ("Lanny ");

2.4 use func to call

Same Method

Public static string hellowenglish (string strenglish)

{

Return "Hellow" + strenglish;

}

 

But we do not need to customize delegate .. As follows:

Func <string, string> F = hellowenglish;

F ("Lanny ");

You can.

2.5

Differences between func and Resource Sharing

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.