委托的三种实现方式

一. 委托的定义
委托是封装方法的类型,
委托的类型由委托的名称定义。 既然它是是一种类型,那么它就可以像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
    {
        //显式定义一个没有返回值,有两个参数的方法的委托
        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

   三. 【代码下载

原文地址:https://www.cnblogs.com/ucos/p/3549655.html