C#中的委託(二)

1、Action<T>和Func<T>委託ide

     除了上篇描述的爲每一個參數和返回類型定義一個新委託類型以外,還能夠使用Action<T>和Func<T>委託。經過T的標示,咱們能夠知道這是兩個泛型委託。this

2、Action<T>spa

     其中Action<T>委託表示引用一個void返回類型的方法。這個委託存在不一樣的變體,能夠傳遞最多16種不一樣的參數類型。如:Action<in T1>表示調用帶一個參數的方法(返回類型爲void),Action<in T1,in T2>表示兩個參數,Action<in T1.....in T16>表示16個參數(最多16個);在這就以上篇中數據操做類,加以改造:code

    public class MathOperation
    {
        public static void Add(int x, int y)
        {
            int result = x + y;
            Console.WriteLine("x+y={0}", result);
        }

        public static void Reduce(int x, int y)
        {
            int result = x - y;
            Console.WriteLine("x-y={0}", result);
        }

        public static void Process(Action<int, int> action, int x, int y)
        {
            action(x, y);
        }
    }

執行代碼:orm

            Action<int, int>[] operation = { 
                                         MathOperation.Add,
                                         MathOperation.Reduce
                                         };

            for (int i = 0; i < operation.Length; i++)
            {
                Console.WriteLine("執行第{0}個委託方法", i);
                for (int j = 1; j < 10; j++)
                {
                    MathOperation.Process(operation[i], i, j);
                    Thread.Sleep(20);
                }
            }

輸出結果:blog

3、Func<T>排序

     Func<T>調用帶返回類型的方法。與Action<T>相似,Func<T>也定義了不一樣的變體,至多能夠16個參數類型和一個返回類型。Func<out TResult>委託類型能夠調用帶返回類型且無參的方法,Func<in T,out TResult>表示帶有一個參數的方法。咱們繼續在數據操做類上改造:ci

    public class MathOperation
    {
        public static int Add(int x, int y)
        {
            int result = x + y;
            return result;
        }

        public static int Reduce(int x, int y)
        {
            int result = x - y;
            return result;
        }

        public static void Process(Func<int, int, int> action, int x, int y)
        {
            int result = action(x, y);
            Console.WriteLine("執行結果爲:{0}", result);
        }
    }

執行代碼:get

            Func<int, int,int>[] operation = { 
                                         MathOperation.Add,
                                         MathOperation.Reduce
                                         };

            for (int i = 0; i < operation.Length; i++)
            {
                Console.WriteLine("執行第{0}個委託方法", i);
                for (int j = 1; j < 10; j++)
                {
                    MathOperation.Process(operation[i], i, j);
                    Thread.Sleep(20);
                }
            }

執行結果:string

4、員工工資排序實例

     首先定義一個員工類

    public class Empolyee
    {
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 工資
        /// </summary>
        public decimal Salary { get; set; }

        public Empolyee(string name, decimal salary)
        {
            this.Name = name;
            this.Salary = salary;
        }

        /// <summary>
        /// 重構
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0}的工資爲:{1}", Name, Salary);
        }

        /// <summary>
        /// 定義一個比較員工工資的靜態方法(用於向通用排序類傳值)
        /// </summary>
        /// <param name="e1"></param>
        /// <param name="e2"></param>
        /// <returns></returns>
        public static bool CompareTo(Empolyee e1, Empolyee e2)
        {
            return e1.Salary < e2.Salary;
        }
    }

     而後建立一個排序類:

    public class SortUtil
    {
        public static void Sort<T>(IList<T> arrList, Func<T, T, bool> compare)
        {
            ////冒泡排序
            //for (int i = 0; i < arrList.Count - 1; i++)
            //{
            //    for (int j = i + 1; j < arrList.Count - 1; j++)
            //    {
            //        if (compare(arrList[i], arrList[j]))
            //        {
            //            var temp = arrList[i];
            //            arrList[i] = arrList[j];
            //            arrList[j] = temp;
            //        }
            //    }
            //}

            bool result = true;
            do
            {
                result = false;
                for (var i = 0; i < arrList.Count - 1; i++)
                {
                    if (compare(arrList[i], arrList[i + 1]))
                    {
                        T temp = arrList[i];
                        arrList[i] = arrList[i + 1];
                        arrList[i + 1] = temp;
                        result = true;
                    }
                }
            }
            while (result);

        }
    }

執行代碼:

            Empolyee[] list = { 
                              new Empolyee("A",12.3M),
                              new Empolyee("B",12.4M),
                              new Empolyee("C",12.2M),
                              new Empolyee("D",12.5M),
                              new Empolyee("E",12.1M)
                              };

            SortUtil.Sort<Empolyee>(list, Empolyee.CompareTo);

            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }

執行結果:

相關文章
相關標籤/搜索