C#中的Array类使用泛型委托做参数

来源:互联网 发布:剑灵客户端优化 编辑:程序博客网 时间:2024/06/16 19:15
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 泛型
  6. {
  7.     class Array使用泛型委托
  8.     {
  9.         public static void Main()
  10.         { 
  11.             //C#从2.0开始对Array类的很多方法,提供了泛型委托参数
  12.             // 例如: Array下的 Sort方法: public static void Sort<T>( T[] array ,Comparison<T> comparison )
  13.             // 使用了下面的Comparison委托做参数
  14.             // public delegate int Comparison<T>(   T x , T y )
  15.             Person[] pers = new Person[] { new Person( "zhao" , 15 ) , new Person( "aladdin" , 30 ) , new Person("jacky" , 20 )};
  16.             Array.Sort<Person>( pers, (p1, p2) => p1.CompareTo( p2) );
  17.             foreach (Person p in pers)
  18.             {
  19.                 Console.WriteLine( p.name + "--" + p.age ); // 15 20 30
  20.             }
  21.             //Array中的ForEach方法
  22.             //public static void ForEach<T>( T[] array, Action<T> action)
  23.             //第二个参数是委托Action
  24.             //public delegate void Action<T>(T obj)
  25.             //这个方法其实就是遍历把第一个元素当参数给委托方法执行一次
  26.             Array.ForEach<Person>( pers , Console.WriteLine ); //每个对象都被输出了一次,因为我们没重写ToString,所以输出的是对象名 
  27.             //如果需要更多的控制,可以用呀妈得(我不会读)表达式
  28.             Array.ForEach<Person>(pers, param => Console.WriteLine(param.name + "--" + param.age));
  29.             //Array中的FindAll方法
  30.             //public static T[] FindAll<T>( T[] array,  Predicate<T> match)
  31.             //使用了委托
  32.             //public delegate bool Predicate<T>(T obj)
  33.             //查找所有以n结尾的对象 目前只有一个 aladdin 
  34.             Person[] rePers = Array.FindAll<Person>(pers, par1 => par1.name.LastIndexOf('n') > 0) ;
  35.             Console.WriteLine( "查找以n结尾的人");
  36.             foreach (Person p in rePers)
  37.             {
  38.                 Console.WriteLine( p.name );
  39.             }
  40.             //ConvertAll方法 将一种对象,转换成为别一种对象。。例:我们要将Person类对象,转换成为Racer,只复制名字,不要年纪
  41.             //public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array,   Converter<TInput, TOutput> converter)
  42.             //使用的泛型委托
  43.             //public delegate TOutput Converter<TInput, TOutput>(TInput input)
  44.             Racer[] races = Array.ConvertAll<Person, Racer>(pers, (param1) => new Racer(param1.name));
  45.             Console.WriteLine( "输出被转换的人");
  46.             foreach (Racer r in races)
  47.             {
  48.                 Console.WriteLine( r.name );
  49.             }
  50.             Console.ReadLine() ;
  51.         }
  52.     }
  53.         
  54.     class Person : IComparable
  55.     {
  56.         public string name;
  57.         public int age;
  58.         public Person(string name, int age)
  59.         {
  60.             this.name = name;
  61.             this.age = age;
  62.         }
  63.         #region IComparable 成员
  64.         public int CompareTo(object obj)
  65.         {
  66.             Person per = obj as Person;
  67.             if (per != null)
  68.             {
  69.                 if (this.age == per.age)
  70.                 {
  71.                     return 0;
  72.                 }
  73.                 else if (this.age > per.age)
  74.                 {
  75.                     return 1;
  76.                 }
  77.                 else
  78.                 {
  79.                     return -1;
  80.                 }
  81.             }
  82.             else 
  83.             {
  84.                 return -1;
  85.             }
  86.         }
  87.         #endregion
  88.     }
  89.     class Racer
  90.     {
  91.         public string name;
  92.         public Racer(string name)
  93.         {
  94.             this.name = name;
  95.         }
  96.     }
  97. }
原创粉丝点击