委托的三种实现⽅式
⼀. 委托的定义
委托是封装⽅法的类型,委托的类型由委托的名称定义。既然它是是⼀种类型,那么它就可以像int、string⼀样去使⽤,好像也没有我们想象的那么神秘。在Entity Framework 3.5之前,在使⽤委托时需要先显⽰的声明委托,格式如下:
public delegate void DelegateName(string message);
View Code
在Entity Framework 3.5之后,新增了两种实现⽅式,格式如下:
1. 不带返回值的委托定义:
public delegate void Action<in T1, in T2>(
T1 arg1,
T2 arg2
)
View Code
2. 代返回值的委托定义:
publicdelegate TResult Func<in T, out TResult>(
T arg
)
View Code
⼆. 委托的具体实现
 ⾸先,显⽰声明委托的实现⽅式:
 1. 声明委托 
//显式定义⼀个没有返回值,有两个参数的⽅法的委托
public delegate void CommonDelegateForAction(string string1, string string2);
View Code
 2. 实例化委托的实例,并将⽅法的引⽤分配给委托实例
//实例化⼀个CommonDelegate的实例,并将对UpperString ⽅法的引⽤分配给委托实例
CommonDelegateForAction commDelegate = Concat;
View Code
 3. 引⽤委托的实例,调⽤引⽤委托的⽅法
//引⽤CommonDelegateForAction的实例,调⽤UpperString⽅法
commDelegate(source1, source2);
View Code
 4. 完整的代码实现
public class CommonDelegateSample
{
variable used in lambda//显式定义⼀个没有返回值,有两个参数的⽅法的委托
public delegate void CommonDelegateForAction(string string1, string string2);
public void SampleForAction()
{
//实例化⼀个CommonDelegate的实例,并将对UpperString ⽅法的引⽤分配给委托实例
CommonDelegateForAction commDelegate = Concat;
string source1 = "I deeply love my country,";
string source2 = "But she didn't love me.";
/
/引⽤CommonDelegateForAction的实例,调⽤UpperString⽅法
commDelegate(source1, source2);
}
///<summary>
///连接两个字符串
///</summary>
///<param name="str1">字符串</param>
///<param name="str2">字符串</param>
private void Concat(string str1, string str2)
{
Console.WriteLine(str1+str2);
}
}
View Code
 其次,Action<T1, T2> 委托实现⽅式
  Action<...>委托最多可以有16个参数,并且Action<...>委托使⽤前不需要提前声明。
 1. 实例化委托的实例,并将⽅法的引⽤分配给委托实例
//定义⼀个委托变量,并赋值为Concat
Action<string, string> actionDelegate = Concat;
View Code
 2. 引⽤委托的实例,调⽤⽅法
//引⽤委托的实例,调⽤⽅法
actionDelegate(str1, str2);
View Code
 3. 完整的代码实现
public void Sample()
{
/
/定义⼀个委托变量,并赋值为Concat
Action<string, string> actionDelegate = Concat;
string str1 = "I deeply love my country,";
string str2 = "But she didn't love me.";
//引⽤委托的实例,调⽤⽅法
actionDelegate(str1, str2);
}
///<summary>
///输出两个字符串的连接
///</summary>
///<param name="str1">字符串</param>
/
//<param name="str2">字符串</param>
private void Concat(string str1,string str2)
{
Console.WriteLine(str1+str2);
}
}
View Code
 最后, Func<T, TResult> 委托的实现⽅式
  Func<...>委托最多可以有16个输⼊参数,⼀个返回值,并且Func<...>委托使⽤前不需要提前声明。
 1. 实例化委托的实例,并将⽅法的引⽤分配给委托实例
//定义⼀个委托变量,并赋值为UpperString
Func<string, string> convert = UpperString;
View Code
  2. 引⽤委托的实例,调⽤⽅法
//⽤委托实例,调⽤⽅法
string target = convert(source);
View Code
  3.  Func<T, TResult> 委托,与Lambda表达式
public void SampleForLambda()
{
Func<string, string> convert = str => str.ToUpper();
string source = "delegate";
string target = convert(source);
Console.WriteLine(target);
}
View Code
  4. Func<T, TResult> 委托,作为变量
/
//<summary>
/// Func<T, TResult>委托,作为变量
///</summary>
public void SampleForVariable()
{
//声明⼀个Func变量,并给这个变量赋值⼀个Lambda表达式
Func<string, string> selector = str => str.ToUpper();
string[] words = { "china", "america", "england", "germany" };
//Select⽅法的定义:IEnumerable<string> IEnumerable<string>.Select<string, string>(Func<string, string> selector);            //Select⽅法需要⼀个Func<string, string>类型的参数,所以传递⼀个Func<string, string>类型的参数selector。
IEnumerable<String> aWords = words.Select(selector);
foreach (String word in aWords)
Console.WriteLine(word);
}
View Code
  5. 完整的代码实现
public class FuncDelegateSample
{
///<summary>
///实例化 Func<T, TResult>委托
/
//</summary>
public void Sample()
{
//定义⼀个委托变量,并赋值为UpperString
Func<string, string> convert = UpperString;
string source = "I deeply love my country,But she didn't love me.";
//⽤委托实例,调⽤⽅法
string target = convert(source);
Console.WriteLine(target);
}
///<summary>
/
// Func<T, TResult>委托,与Lambda表达式
///</summary>
public void SampleForLambda()
{
Func<string, string> convert = str => str.ToUpper();
string source = "delegate";
string target = convert(source);
Console.WriteLine(target);
}
///<summary>
/// Func<T, TResult>委托,作为变量
/
//</summary>
public void SampleForVariable()
{
//声明⼀个Func变量,并给这个变量赋值⼀个Lambda表达式
Func<string, string> selector = str => str.ToUpper();
string[] words = { "china", "america", "england", "germany" };
//Select⽅法的定义:IEnumerable<string> IEnumerable<string>.Select<string, string>(Func<string, string> selector);            //Select⽅法需要⼀个Func<string, string>类型的参数,所以传递⼀个Func<string, string>类型的参数selector。
IEnumerable<String> aWords = words.Select(selector);
foreach (String word in aWords)
Console.WriteLine(word);
}
///<summary>
///将输⼊字符串的字符转化为⼤写
///</summary>
///<param name="str">输⼊参数</param>
///<returns>返回值</returns>
private string UpperString(string str)
{
return str.ToUpper();
}
}
View Code
三. 【】

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。