A brief introduction to C # delegates in asp.net

Source: Internet
Author: User

A reference to the Commission, floating now in our mind is probably listening to the most is similar to C + + function pointers, hehe, at least my first reaction is this.

As for the definition and use of delegates, there have been many people who have explained and nuanced, especially the Zhang Ziyang one. I don't have to talk about it any more.

What I'm going to say today is three ways of delegating in C #: Func Delegates, action delegates, predicate delegates, and common usage scenarios for these three types of delegates.

Func,action,predicate Comprehensive Analysis

First, to illustrate the Func delegate, we can learn from MSDN that there are 5 types of func delegates:

(1) *delegate TResult func<tresult> ();
(2) *delegate TResult func<t1,tresult> (T1 arg1);
(3) *delegate TResult func<t1,t2,tresult> (T1 arg1, T2 arg2);
(4) *delegate TResult func<t1,t2,t3,tresult> (T1 arg1, T2 arg2, T3 arg3);
(5) *delegate TResult func<t1,t2,t3,t4,tresult>t1 arg1, T2 arg2, T3 arg3, T4 arg4);
where (1) a function that can only delegate an TResult but has a return value is the return type.

The (2) can delegate only one incoming parameter, a function with a return value, T1 as an incoming parameter, and TResult as the return type.

(3) Only two incoming parameters can be delegated, functions with return values, T1 and T2 are two incoming parameters, TResult are return types, (4) and (5), and so on.

So how to use it? A few simple examples are given below:

The code is as follows Copy Code
#region Func Delegate

The use of func<tresult>
Here TResult represents the return value type of the function
A parameterless function with only the proxy return value of type TResult
func<string> Func = Delegate ()
{
Return "I am func<tresult> commissioned the result";
};
Console.WriteLine (func ());
Console.readkey ();
The use of func<t,tresult>
Here the T is the incoming type of the function of the proxy, and TResult represents the return value type of the function
Only the proxy parameter is type T, and a function that returns a value of type TResult
func<string, string> Funcone = Delegate (string s)
{
return S.toupper ();
};
Console.WriteLine (Funcone ("I was the result of func<t,tresult>"));
Console.readkey ();
The use of func<t1,t2,tresult>
Here T1,t2 is the incoming type of the function of the proxy, TResult represents the return value type of the function
Only the proxy parameter is a T1,T2 type, and a function that returns a value of type TResult
Func<string, String, string> Functwo = Delegate (string value1, String value2)
{
return value1 + "" + value2;
};
Console.WriteLine (Functwo ("I Am", "func<t1,t2,tresult> commissioned results"));
Console.readkey ();
#endregion

In the code above, I use an anonymous method instead of a function where delegate () represents a parameterless function, and delegate (string s) represents a function that has an incoming argument, and so on.

Then you need to explain the action delegate, which is also very common, especially when it comes to threading and interface interaction, and it's very convenient to interact with Lamada expressions. I'll refer to usage later.

Look at several forms of action delegation:

(1) * delegate void Action (); No parameters, no return value
(2) * delegate void action<t> (T1 arg1);
(3) * delegate void action<t1,t2> (T1 arg1, T2 arg2);
(4) * delegate void Action<t1,t2,t3>t1 arg1, T2 arg2, T3 arg3);
(5) * delegate void Action<t1,t2,t3,t4>t1 arg1, T2 arg2, T3 arg3, T4 arg4);
As you can see from the above, there are a total of 5 representations, of which (1) have neither passed in parameters, there is no return value, then it is suitable for proxy those without parameter, no return value of the function, (2) There is an incoming parameter, no return value, suitable for proxy has parameters, no return value of functions, (3) (4) (5) and so on. Most of the four incoming parameters are accommodated.

So how to use it? Here are some simple examples:

  code is as follows copy code

#region The use of the Action
///action<t>
///where T is the incoming type of the proxy function, no return value
action<string[]> Action = Delegate (string[) x)
{
    var result = from P in x
     where P.contains ("s")
 &nbs p;   select P;
    foreach (string s in result.) ToList ())
    {
        Console.WriteLine (s);
    }
};
string[] str={"Charlies", "Nancy", "Alex", "Jimmy", "Selina"};
Action (str);
Console.readkey ();
#endregion

The example above is an array of string types passed in, finding the item that contains the character s, and outputting it to the console.

The last one is the predicate delegate, this form is less, is an incoming parameter, the return value is bool type, specific examples are as follows:

The code is as follows Copy Code
#region predicate
Usage of bool Predicate<t>
Enter a parameter of type T with the return value of type bool
predicate<string[]> predicate = delegate (string[] x)
{
var result = from P in X
Where P.contains ("s")
Select P;
if (result. ToList (). Count > 0)
{
return true;
}
Else
{
return false;
}
};
String[] _value = {"Charlies", "Nancy", "Alex", "Jimmy", "Selina"};
if (predicate (_value))
{
Console.WriteLine ("They contain.");
}
Else
{
Console.WriteLine ("They don t contain.");
}
Console.readkey ();
#endregion

The code above is also to determine if there is an item in the string array that contains S, and then print out They contain in the console. If not, print out They don ' t contain.

To sum up, these three features are:

Func can accept 0 to 4 incoming parameters and must have a return value

Action can accept 0 to 4 incoming parameters, no return value

predicate can only accept one incoming parameter, the return value is bool type

The code is as follows Copy Code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Delegateintegrateconsoleapp
{
Class Program
{
static void Main (string[] args)
{
#region Func Delegate

The use of func<tresult>
Here TResult represents the return value type of the function
A parameterless function with only the proxy return value of type TResult
func<string> Func = Delegate ()
{
Return "I am func<tresult> commissioned the result";
};
Console.WriteLine (func ());
Console.readkey ();
The use of func<t,tresult>
Here the T is the incoming type of the function of the proxy, and TResult represents the return value type of the function
Only the proxy parameter is type T, and a function that returns a value of type TResult
func<string, string> Funcone = Delegate (string s)
{
return S.toupper ();
};
Console.WriteLine (Funcone ("I was the result of func<t,tresult>"));
Console.readkey ();
The use of func<t1,t2,tresult>
Here T1,t2 is the incoming type of the function of the proxy, TResult represents the return value type of the function
Only the proxy parameter is a T1,T2 type, and a function that returns a value of type TResult
Func<string, String, string> Functwo = Delegate (string value1, String value2)
{
return value1 + "" + value2;
};
Console.WriteLine (Functwo ("I Am", "func<t1,t2,tresult> commissioned results"));
Console.readkey ();
/************* the remaining similar operations above, you can accept up to four incoming parameters ***************
*delegate TResult func<tresult> ();
*delegate TResult func<t1,tresult> (T1 arg1);
*delegate TResult func<t1,t2,tresult> (T1 arg1, T2 arg2);
*delegate TResult func<t1,t2,t3,tresult> (T1 arg1, T2 arg2, T3 arg3);
*delegate TResult func<t1,t2,t3,t4,tresult>t1 arg1, T2 arg2, T3 arg3, T4 arg4);
*/
#endregion
The use of #region action
The use of action<t>
Here the T is the incoming type of the proxy function, no return value
action<string[]> action = delegate (string[] x)
{
var result = from P in X
Where P.contains ("s")
Select P;
foreach (string s in result.) ToList ())
{
Console.WriteLine (s);
}
};
String[] str={"Charlies", "Nancy", "Alex", "Jimmy", "Selina"};
Action (str);
Console.readkey ();
/*************** the remaining similar operations above, you can accept up to four incoming parameters **********
* delegate void Action (); No parameters, no return value
* Delegate void Action<t> (T1 arg1);
* Delegate void Action<t1,t2> (T1 arg1, T2 arg2);
* Delegate void Action<t1,t2,t3>t1 arg1, T2 arg2, T3 arg3);
• Delegate void Action<t1,t2,t3,t4>t1 arg1, T2 arg2, T3 arg3, T4 arg4);
*/
#endregion
#region predicate
Usage of bool Predicate<t>
Enter a parameter of type T with the return value of type bool
predicate<string[]> predicate = delegate (string[] x)
{
var result = from P in X
Where P.contains ("s")
Select P;
if (result. ToList (). Count > 0)
{
return true;
}
Else
{
return false;
}
};
String[] _value = {"Charlies", "Nancy", "Alex", "Jimmy", "Selina"};
if (predicate (_value))
{
Console.WriteLine ("They contain.");
}
Else
{
Console.WriteLine ("They don t contain.");
}
Console.readkey ();
#endregion
}
}
}
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.