C#学习之委托(2)

来源:互联网 发布:c语言开发实战宝典 编辑:程序博客网 时间:2024/06/14 16:22

Action<T>和Func<T>委托

除了为每个参数和返回类型定义一个新委托类型之外,还可以使用Action<T>和Func<T>委托。泛型Action<T>委托表示引用一个void返回类型的方法,因为这个委托存在不同的变体,所以可以传递至多16种不同的参数类型,没有泛型参数的Action类可调用没有参数的方法,例如Action<in T>代表调用带一个参数的方法,Action<int T1,in T2>调用两个参数的方法。
Func<T>委托可以以类似的方法使用。Func<T>允许调用带返回类型的方法。与Action<T>类似,Func<T>也定义了不同的变体,至多也可以传递16个参数类型和一个返回类型。Func<out TResult>委托类型可以调用待返回类型且无参数的方法,Func<in T,out TResult>调用带一个参数的方法。
接C#学习之委托(1)当中的例子。将其中的main方法改一下
 static void Main(string[] args)        {            Func<double, double>[] operations =             {                MathTool.MutipleByTwo,                MathTool.square            };            ProcessAndDisplay(operations[0], 1.7);            ProcessAndDisplay(operations[1], 1.7);            Console.ReadKey();        }        public static void ProcessAndDisplay(Func<double, double> action, double value)        {            double result = action(value);            Console.WriteLine("Value is {0},The Result is {1}",value,result);        }

BubbleSorter示例

在这里再举例说明一下委托的一个用途。就我们熟知的冒泡排序算法,是一种简单的排序算法,适合于一小组数字,因为对于大量的数字,还有更高效的算法。冒牌排序算法重复遍历数组,比较每一对熟知,按照需要交换它们的位置,把最大的数字逐步移动到数组的组后。我们最常见的冒泡排序是对int排序,但是有时候我们希望使用排序算法给任何对象排序,换言之,就是对包含Currency结构数组或者自定义的其他类和结构排序,这样的话对于int类型的比较方法“>”、"=="、“<”等操作就不能满足其他的对象,所以在这里可以使用委托,在委托中传递一个封装的方法,这个方法可以用来比较这种类型的对象。
对于接受类型T的泛型方法Sort<T>(),需要一个比较方法,其两个参数的类型是T,if比较返回类型是bool类型,这个方法可以从Func<T1,T2,TResult>委托中引用,其中T1和T2类型相同。
program.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Employee[] employees =             {                new Employee("Person",20000),                new Employee("James",4500),                new Employee("Tom",12450),                new Employee("Ross",25000),                new Employee("Duck",10000)            };            BubbleSorter.Sort(employees, Employee.CompareSalary);            foreach (var employee in employees)            {                Console.WriteLine(employee.ToString());            }            Console.ReadKey();        }    }}

BubbleSorter.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class BubbleSorter    {        public static void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison)        {            for (int j = 0; j < sortArray.Count - 1; j++)            {                for (int i = 0; i < sortArray.Count - 1; i++)                {                    if (comparison(sortArray[i+1], sortArray[i]))                    {                        T temp = sortArray[i];                        sortArray[i] = sortArray[i + 1];                        sortArray[i + 1] = temp;                    }                }            }        }    }}

Employee.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Employee    {        private string name;        private decimal salary;        public Employee(string name, decimal salary)        {            this.name = name;            this.salary = salary;        }        public string Name        {            set { name = value; }            get { return name; }        }        public decimal Salary        {            set { salary = value; }            get { return salary; }        }        public override string ToString()        {            return string.Format("{0},{1:C}",Name,Salary);        }        public static bool CompareSalary(Employee e1, Employee e2)        {            return e1.Salary < e2.Salary;        }    }}


0 0
原创粉丝点击