Action委托和Func委托
除了我们自己定义的委托之外,系统还给我们提供过来一个内置的委托类型,Action和Func
Action委托
Action委托引用了一个void返回类型的方法,T表示方法参数,它可以传递0或者多到16个参数类型,但不能有返回值
Action<in T>
Action<in T1,in T2>
Action<in T1,in T2 .... in T16>
static void PrintString()
{
Console.WriteLine("hello world.");
}
static void PrintInt(int i)
{
Console.WriteLine(i);
}
static void PrintString(string str)
{
Console.WriteLine(str);
}
static void PrintDoubleInt(int i1, int i2)
{
Console.WriteLine(i1+i2);
}
static void Main(string[] args)
{
//Action a = PrintString;//action是系统内置(预定义)的一个委托类型,它可以指向一个没有返回值,没有参数的方法
//Action<int> a=PrintInt;//定义了一个委托类型,这个类型可以指向一个没有返回值,有一个int参数的方法
//Action<string> a = PrintString;//定义了一个委托类型,这个类型可以指向一个没有返回值,有一个string参数的方法 在这里系统会自动寻找匹配的方法
Action<int, int> a = PrintDoubleInt;//action可以后面通过泛型去指定action指向的方法的多个参数的类型 ,参数的类型跟action后面声明的委托类型是对应着的
a(34, 23);
}
Func委托
Func引用了一个带有一个返回值的方法,它可以传递0或者多到16个参数类型,和一个返回类型
Func<out TResult>
Func<in T,out TResult>
Func<in T1,in T2,,,,,,in T16,out TResult>
static int Test1()
{
return 1;
}
static int Test2(string str)
{
Console.WriteLine(str);
return 100;
}
static int Test3(int i, int j)
{
return i + j;
}
static void Main(string[] args)
{
//Func<int> a = Test1;//func中的泛型类型制定的是 方法的返回值类型
//Console.WriteLine(a());
//Func<string, int> a = Test2;//func后面可以跟很多类型,最后一个类型是返回值类型,前面的类型是参数类型,参数类型必须跟指向的方法的参数类型按照顺序对应
Func<int, int, int> a = Test3;//func后面必须指定一个返回值类型,参数类型可以有0-16个,先写参数类型,最后一个是返回值类型
int res = a(1, 5);
Console.WriteLine(res);
}
案例:
delegate double DoubleOp(double x);
//如何用Func表示
Func<double,double>
对int类型排序
对集合进行排序,冒泡排序
bool swapped = true;
do{
swapped = false;
for(int i =0;i<sortArray.Length -1;i++){
if(sortArray[i]>sortArray[i+1]){
int temp= sortArray[i];
sortArray[i]=sortArray[i+1];
sortArray[i+1]=temp;
swapped = true;
}
}
}while(swapped);
这里的冒泡排序只适用于int类型的,如果我们想对他进行扩展,这样它就可以给任何对象排序。
比如有个雇员类
class Employee{
public Employee(string name,decimal salary){
this.Name = name;
this.Salary = salary;
}
public string Name{get;private set;}
public decimal Salary{get;private set;}
public static bool CompareSalary(Employee e1,Employee e2){
return e1.salary>e2.salary;
}
}
这样就需要用到委托
public static void Sort<T>( List<T> sortArray,Func<T,T,bool> comparision ){
bool swapped = true;
do{
swapped = false;
for(int i=0;i<sortArray.Count-1;i++){
if(comparision(sortArray[i+1],sortArray[i])){
T temp = sortArray[i];
sortArray[i]=sortArray[i+1];
sortArray[i+1]=temp;
swapped = true;
}
}
}while(swapped);
}
通过委托对非int类型对象进行排序
static void Main(){
Employee[] employees = {
new Employee("Bunny",20000),
new Employee("Bunny",10000),
new Employee("Bunny",25000),
new Employee("Bunny",100000),
new Employee("Bunny",23000),
new Employee("Bunny",50000),
};
Sort(employees,Employee.CompareSalary);
输出
}
using System;
using System.Collections.Generic;
namespace SortWithDelegateTest
{
class MainClass
{
private static void Sort(int[] array) {
bool swarpped = true;
do {
swarpped = false;
for (int i = 0; i < array.Length - 1; i++) {
if (array [i] > array [i + 1]) {
int temp = array [i];
array [i] = array [i + 1];
array [i + 1] = temp;
swarpped = true;
}
}
} while(swarpped);
}
public static void Sort<T> (List<T> needSortList, Func<T,T,bool> comparision) {
bool swarpped = true;
do {
swarpped = false;
for(int i = 0; i < needSortList.Count - 1; i++) {
if(comparision(needSortList[i],needSortList[i + 1])) {
T temp = needSortList[i];
needSortList[i] = needSortList[i + 1];
needSortList[i + 1] = temp;
swarpped = true;
}
}
} while(swarpped);
}
public static void Main (string[] args)
{
int[] array = {111,333,555,222,666,444};
Sort (array);
foreach(int num in array) {
Console.WriteLine(num);
}
List<Student> students = new List<Student> {
new Student ("张三", 56),
new Student ("李四", 43),
new Student ("王五", 63),
new Student ("马六", 87),
new Student ("吴七", 36),
new Student ("赵八", 13),
new Student ("刘九", 90),
new Student ("买买提", 44)
};
Sort<Student> (students, Student.CompareSalary);
foreach (Student student in students) {
Console.WriteLine ("学生姓名:{0},学生学号:{1}", student.Name, student.No);
}
}
}
class Student {
public string Name { get; private set; }
public int No { get; private set; }
public Student(string name, int no) {
Name = name;
No = no;
}
public static bool CompareSalary(Student s1, Student s2) {
return s1.No > s2.No;
}
}
}