Asp Tutorial: Delegate, Action, Func, and Predicate in. net development
Many new programs, such as Linq and Lamda expressions, are poorly written. The use and differences of related Delegate, Action, Func, and Predicate are often confusing. Here we will describe it in detail based on the actual application.
Delegation is often used when writing code. This is common in winform. However, when custom Delegate is used, we often find that Delegate must be globally visible before it can be used as needed, it seems inconvenient to use private delegate objects in this class. Let's look at the traditional Delegate method.
Public delegate void MyDelegate (string name );
Public class MyBlogBase
{
Private MyDelegate mydelegate;
}
Make sure that MyDelegate is placed outside the class to be visible in other places, and the appearance of Action and Func has changed this situation. The two are actually Delegate defined by the system, he has a lot of heavy-load methods to facilitate calls in various application cases. It is globally visible in the System namespace. The following describes the Action, which has multiple overload values. The Action <T> below is used as an example.
Action <T>: encapsulate a method. This method has only one parameter and does not return a value. T is any type that can be received. The code is as follows:
Public class MyBlogBase
{
Public string myName;
Action <string> myAction;
Public MyBlogBase ()
{
// MyAction = delegate (string curName) {myName = curName ;};
// MyAction = new Action <string> (SetAction );
MyAction = curname => {myName = curname ;};
}
Private void SetAction (string name)
{
MyName = name;
}
}
In the preceding example, three methods are provided to use Action. Method 1: Anonymous delegation, and Method 2: specify an actual method. Method 3: Use the Lamda expression. You can run all the preceding three operations.
In practice, it is easier and more flexible than the original Delegate definition. What about Func?
Func <T in, T Tresult>: encapsulate a method that has a parameter and returns the type value specified by the TResult parameter. In fact, the difference between Func and Action is obvious and direct. Both are delegates, but Func can return the function execution result, and the Action return type is Void. The difference is obvious. In a specific project, it is easy to determine which one to use. The code call for the specific Func is described below:
Public string myName;
Func <string, string> myFunc;
Public MyBlogBase ()
{
// MyFunc = delegate (string curName) {return curName. ToUpper ();};
// MyFunc = new Func <string, string> (SetFunc );
MyFunc = name => {return name. ToUpper ();};
}
Private string SetFunc (string name)
{
Return name. ToUpper ();
}
Public void StartFun (string curName)
{
MyName = myFunc (curName );
}
The above three methods are suitable for the definition of Func. You can choose the appropriate programming mode. In fact, the anonymous method has the advantage that you can directly use the variables in the current function, the code is more concise, but some people may find it difficult to read it.
Predicate <T>: A delegate that defines a set of conditions and determines whether the specified object meets these conditions. this method is often used in the search of sets, such as arrays and regular expression matching results sets. This method is quick and convenient. The code is as follows:
Predicate <int> myPredicate;
Int [] myNum = new int [8] {12, 33, 89, 21, 15, 29, 40, 52 };
Public int [] myResult;
Public MyBlogBase ()
{
MyPredicate = delegate (int curNum)
{If (curNum % 2 = 0) return true;
Else return false;
};
}
Public void StartPredicate ()
{
MyResult = Array. FindAll (myNum, myPredicate );
}
The preceding example illustrates the use of Predicate. In the FindAll method, parameter 2 is a Predicate. In the specific execution, the specified method is executed for each array element. If the requirements are met, true is returned, it will be stored in the result set. If it does not match, it will be removed. The final returned set is the set desired after the result is judged. The application scenario of this method is like yield in the iteration. Of course, this method can also be written in a way similar to Action and Func 3 above, which is omitted here.
To better verify the running effect, add the Test project and perform the Test. Paste the code and share it with you:
[TestMethod]
Public void TestAction ()
{
MyBlogBase blogObj = new MyBlogBase ();
BlogObj. StartAction ("ywg369 ");
Assert. AreEqual ("ywg369", blogObj. myName );
}
[TestMethod]
Public void TestFunc ()
{
MyBlogBase blogObj = new MyBlogBase ();
BlogObj. StartFun ("ywg369 ");
Assert. AreEqual ("YWG369", blogObj. myName );
}
[TestMethod]
Public void TestPredicate ()
{
MyBlogBase blogObj = new MyBlogBase ();
BlogObj. StartPredicate ();
Assert. AreEqual (3, blogObj. myResult. Length );
}
After verification, it runs well and each method runs successfully according to the expected results. Here, we have a general understanding of the use of Delegate, Action, Func, and Predicate.