Original http://www.cnblogs.com/skm-blog/archive/2013/05/24/3096294.html
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:
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 class. The Action and Func generic delegation described in this article are actually derived types of the MulticastDelegate class. In C #, when we use the delegate key word to declare a delegate type, the C # compiler will help us generate a signature that matches the signature according to the method signature we declare, 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:
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:
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 16. Therefore, Action and its generic delegate can match any function with no return value and the number of parameters ranging from 0 to 16. Similarly, the Func generic delegate can match any function with a value ranging from 0 to 16. In general, the number of function parameters in the program will not exceed 16. Even if there are more than 16 parameters, we can declare a new generic delegate type to deal with it.
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, we 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.
I strongly recommend you look at this article Zhang Ziyang: http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html