标签:
1 static class SimpleSort1 2 { 3 public static void BubbleSort(int[] items) 4 { 5 int i = 0, j = 0, temp = 0; 6 if (items == null) 7 { 8 return; 9 } 10 for (i = items.Length - 1; i >= 0; i--) 11 { 12 for (j = 1; j <= i; j++) 13 { 14 if (items[j - 1] > items[i]) 15 { 16 temp = items[j - 1]; 17 items[j - 1] = items[i]; 18 items[i] = temp; 19 } 20 } 21 22 } 23 } 24 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 int[] arr = new int[] { 1, 2, 5, 645, 4, 6, 73, 5, 3, 6, 7, 66 }; 7 SimpleSort1.BubbleSort(arr, SortType.Ascending); 8 string str = ""; 9 for (int i = 0; i < arr.Length; i++) 10 { 11 str += arr[i] + ","; 12 13 } 14 Console.WriteLine(str); 15 16 str = ""; 17 SimpleSort1.BubbleSort(arr, SortType.Descending); 18 for (int i = 0; i < arr.Length; i++) 19 { 20 str += arr[i] + ","; 21 22 } 23 Console.WriteLine(str); 24 Console.ReadLine(); 25 26 } 27 } 28 enum SortType 29 { 30 Ascending, 31 Descending 32 } 33 static class SimpleSort1 34 { 35 public static void BubbleSort(int[] items, SortType sorttype) 36 { 37 int i = 0, j = 0, temp = 0; 38 if (items == null) 39 { 40 return; 41 } 42 for (i = items.Length - 1; i >= 0; i--) 43 { 44 for (j = 1; j <= i; j++) 45 { 46 switch (sorttype) 47 { 48 case SortType.Ascending: 49 if (items[j - 1] > items[i]) 50 { 51 temp = items[j - 1]; 52 items[j - 1] = items[i]; 53 items[i] = temp; 54 } 55 break; 56 case SortType.Descending: 57 if (items[j - 1] < items[i]) 58 { 59 temp = items[j - 1]; 60 items[j - 1] = items[i]; 61 items[i] = temp; 62 } 63 break; 64 65 } 66 67 } 68 69 } 70 71 72 } 73 } 74
1 class DelegateSample 2 { 3 public delegate bool ComparisonHandler(int first, int second); 4 //相当于创建了一个数据类型:DelegateSample.ComparisonHandler 5 //因为它被定义成嵌套在DelegateSample中的一个类型。 6 7 } 8 虽然所有委托数据类型都间接从System.Delegate派生,但C#编译器并不允许定义一个直接或间接 9 从System.Delegate派生的类。 10 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 16 int[] arr = new int[] { 1, 32, 5, 645, 4, 6, 73, 5, 36, 61, 7, 66 }; 17 string str = ""; 18 //调用方法时,将指定的函数作为实际参数使用。使用方法来创建一个委托变量,委托是一个引用类型,但不必 19 //用new来实例化它。直接传递名称,而不是显式实例化,这是自C#2.0开始支持的一个新语法,称为委托推断 delegate interface 20 //采用这个语法,编译器将根据方法名来查找方法签名,并验证它同方法的参数类型匹配。 21 SimpleSort1.BubbleSort(arr, SimpleSort1.GreaterThan); 22 for (int i = 0; i < arr.Length; i++) 23 { 24 str += arr[i] + ","; 25 26 } 27 Console.WriteLine(str); 28 29 str = ""; 30 SimpleSort1.BubbleSort(arr, SimpleSort1.LonwerThan); 31 for (int i = 0; i < arr.Length; i++) 32 { 33 str += arr[i] + ","; 34 35 } 36 Console.WriteLine(str); 37 38 str = ""; 39 SimpleSort1.BubbleSort(arr, SimpleSort1.CharThan); 40 for (int i = 0; i < arr.Length; i++) 41 { 42 str += arr[i] + ","; 43 44 } 45 Console.WriteLine(str); 46 47 Console.ReadLine(); 48 49 50 51 } 52 } 53 54 static class SimpleSort1 55 { 56 //使用委托数据类型 声明一个变量作为形式参数 57 public static void BubbleSort(int[] items, DelegateSample.ComparisonHandler compareMethod) 58 { 59 int i = 0, j = 0, temp = 0; 60 if (items == null) 61 { 62 return; 63 } 64 for (i = items.Length - 1; i >= 0; i--) 65 { 66 for (j = 1; j <= i; j++) 67 { 68 if (compareMethod(items[j - 1], items[i])) 69 { 70 temp = items[j - 1]; 71 items[j - 1] = items[i]; 72 items[i] = temp; 73 } 74 } 75 } 76 } 77 78 79 //以下四个函数都与数据类型DelegateSample.ComparisonHandler(委托) 具有同样的签名 80 public static bool GreaterThan(int first, int second) 81 { 82 return first > second; 83 } 84 public static bool LonwerThan(int first, int second) 85 { 86 return first < second; 87 } 88 public static bool CharThan(int first, int second) 89 { 90 int flag = (first.ToString()).CompareTo(second.ToString()); 91 return (flag > 0) ? true : false; 92 } 93 } 94 class DelegateSample 95 { 96 public delegate bool ComparisonHandler(int first, int second); 97 //相当于创建了一个数据类型:DelegateSample.ComparisonHandler 98 99 }
1 DelegateSample.ComparisonHandler compareMethod; 2 compareMethod = 3 delegate(int first, int second) 4 { 5 return first > second; 6 }; 7 SimpleSort1.BubbleSort(arr, compareMethod);
1 SimpleSort1.BubbleSort(arr, 2 delegate(int first, int second) 3 { 4 return first > second; 5 } 6 );
在任何情况下,匿名方法的参数和返回值类型都必须兼容于相对应的委托类型。
1 System.Func<int, int, bool> compareMethodFun; 2 compareMethodFun = 3 delegate(int first, int second) 4 { 5 return first > second; 6 };
1 Action<Object> broadAction = delegate(Object o) 2 { 3 Console.WriteLine(o); 4 }; 5 Action<String> narrowAction = broadAction; 6 7 Func<string> narrowFunction = delegate() 8 { 9 return Console.ReadLine(); 10 }; 11 12 Func<Object> broadFunction = narrowFunction;
1 Func<Object, String> narrowFunction = delegate(Object obj) 2 { 3 4 return obj.ToString(); 5 }; 6 7 Func<String, Object> broadFunction = narrowFunction;
1 SimpleSort1.BubbleSort(arr, 2 (int first, int second) => 3 { 4 //可以有多个语句 5 return first > second; 6 } 7 );
1 SimpleSort1.BubbleSort(arr, 2 (first, second) => 3 { 4 return first > second; 5 } 6 );
1 IEnumerable<Process> processes = Process.GetProcesses().Where( 2 process => { return process.WorkingSet64 > (2 ^ 30); });
1 SimpleSort1.BubbleSort(arr, (first, second) => first > second); 2 SimpleSort1.BubbleSort(arr, (int first, int second) => first > second);
1 System.Linq.Expressions.Expression<Func<int, int, bool>> expression; 2 expression = (x, y) => x > y;
十二、C# 委托与Lambda表达式(匿名方法的另一种写法)
标签:
原文地址:http://www.cnblogs.com/tlxxm/p/4620050.html