In-depth analysis using Action and Func generic delegation in. NET

Source: Internet
Author: User
Tags mscorlib

Delegation plays an extremely important role in C # programming. A delegate can encapsulate a function into a delegate object, and multiple delegates can be combined into one delegate, the delegate object can be stored and transferred like a normal object, and then called at any time. Therefore, the implementation of the function callback mechanism in C # basically depends on the delegate. The delegate keyword of C # is used to declare the delegate. It has the ability to map the declarative delegate type to System. delegate class capability, System. the Delegate class is located in mscorlib. dll, yes. NET. The delegate keyword is used to declare a Delegate. In essence, the derived class of System. delegate is created. Therefore, the Delegate type is not a struct or other type, and it is a class. A delegate object is an instance of a class. The Delegate class declaration is as follows:
Copy codeThe Code is as follows:
Public abstract class Delegate

Delegate is the base class of the Delegate type. The multicast Delegate in C # is actually the MulticastDelegate class, which is the System. delegate's derived class. The Action and Func generic delegation described in this article are actually all derived types of the MulticastDelegate class. In C #, when we use the delegate keyword to declare a delegate type, the C # compiler uses the method signature we declare to help us generate a signature match, class derived from MulticastDelegate. Before a large number of generic applications, we may use the delegate keyword to declare many delegate types when writing a C # program, because these types correspond to different method signatures. You can view mscorlib In the Object Browser of Visual Studio to see the two important generic delegation:

 

 

 


In addition to Action, all other delegates are generic, which are actually some generic classes. This is all generic delegation in the. NET core library. These generic delegation can be divided into Func AND Action. With the help of the generic feature, they can replace almost all Delegate types in C #. That is to say, in general, in our program, we can wrap all functions without declaring any new delegate types. For example, we have two methods:
Copy codeThe Code is as follows:
Public static void OtputString (string str)
{
Console. WriteLine (str );
}
Public static int Add (int a, int B)
{
Return a + B;
}

Compared with Action, Func generic delegation has a TResult type parameter, which is used when a function has a returned value. Action generic delegation is used for a function without a returned value. When we want to obtain the delegate objects of these two methods, this can be changed:
Copy codeThe Code is as follows:
Var action = new Action <string> (OtputString );
Action ("OutputString Invoked! ");
Var func = new Func <int, int, int> (Add );
Var sum = func (3, 5 );
Console. WriteLine (sum );

We can see that Func is used when we wrap functions with return values into delegate objects. If the function does not return values, Action is used, the generic delegate type parameters provided by the core database are as short as 0 and as long as 8. Therefore, Action and its generic delegate can match any function with no return value and the number of parameters ranging from 0 to 8. Similarly, the Func generic delegate can match any function with a value ranging from 0 to 8. In general, the number of function parameters in the program will not exceed 8. Even if there are more than 8 parameters, we can declare a new generic delegate type to deal with this problem.
Copy codeThe Code is as follows:
Delegate void Action <T1, T2, T3, T4, T5, T6, T7, T8, T9> (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, t6 p6, T7 p7, T8 p8, T9 p9 );

Using these generic delegation will not cause any performance loss, so that the use style of delegation in the program remains consistent. The only drawback is that the type name cannot express the specific purpose. For example, the EventHandler delegate is known as the delegate used for event processing. However, when you use the Action <object, EventArgs> delegate, you cannot see the purpose of this type of delegate from the name.
Generic delegation has the ability to replace all other delegates. In which case should I use generic delegation, General Delegation, when to use it, or under which circumstances? Different people may have different introductions, however, in the end, generic delegation can unify the program code style and make it easy to use anywhere.

Related Article

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.