标签:generic code line text main linq log lin console
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 泛型委托 { //委托中使用泛型 public delegate int DeleCompare<T>(T t1,T t2); class Program { static void Main(string[] args) { int[] nums = { 1, 2,10, 3, 4, 5 }; object maxInt = GetMax<int>(nums, CompareInt); string[] names = { "ac", "dgjd", "dsfjeirjgrioeg","cdd" }; //1 使用匿名函数 //object maxString = GetMax<string>(names, delegate(string s1, string s2) //{ // return s1.Length - s2.Length; //}); //2 使用lamda表达式 object maxString = GetMax<string>(names, (string s1, string s2) => { return s1.Length - s2.Length; }); Console.WriteLine(maxInt); Console.WriteLine(maxString); Console.ReadKey(); } public static T GetMax<T>( T[] nums,DeleCompare<T> dele) { T max = nums[0]; for( int i = 0 ; i < nums.Length; i++) { if(dele(max,nums[i]) < 0) { max = nums[i]; } } return max; } public static int CompareInt(int num1,int num2) { return num1 - num2; } } }
标签:generic code line text main linq log lin console
原文地址:http://www.cnblogs.com/my-cat/p/7830981.html