.Net2.0的集合操作 --- What i know?
.Net2.0中提供的Array类 
 namespace System {
namespace System {  public abstract class Array {
  public abstract class Array {   public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] input, Converter<TInput, TOutput> cc);
    public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] input, Converter<TInput, TOutput> cc);  public static bool Exists<T>(T[] array, Predicate<T> match);
    public static bool Exists<T>(T[] array, Predicate<T> match);  public static T Find<T>(T[] array, Predicate<T> match);
    public static T Find<T>(T[] array, Predicate<T> match);  public static T FindLast<T>(T[] array, Predicate<T> match);
    public static T FindLast<T>(T[] array, Predicate<T> match);  public static T[] FindAll<T>(T[] array, Predicate<T> match);
    public static T[] FindAll<T>(T[] array, Predicate<T> match);  public static int FindIndex<T>(T[] array, Predicate<T> match);
    public static int FindIndex<T>(T[] array, Predicate<T> match);  public static int FindLastIndex<T>(T[] array, Predicate<T> match);
    public static int FindLastIndex<T>(T[] array, Predicate<T> match);  public static void ForEach<T>(T[] array, Action<T> action);
    public static void ForEach<T>(T[] array, Action<T> action);  public static void Sort<T>(T[] array, Comparison<T> comparer);
    public static void Sort<T>(T[] array, Comparison<T> comparer);  public static bool TrueForAll<T>(T[] array, Predicate<T> match);
    public static bool TrueForAll<T>(T[] array, Predicate<T> match);  }
  }  }
} 
以上的集合操作使用到了下面的一些预先定义好的代理.(从他们的名字,你就可以明白他们是干什么的)  
 namespace System {
namespace System {  public delegate void    Action<T>(T item);
  public delegate void    Action<T>(T item);  public delegate int     Comparer<T>(T first, T second); // result works like strcmp
  public delegate int     Comparer<T>(T first, T second); // result works like strcmp  public delegate TOutput Converter<TInput, TOutput>(TInput input);
  public delegate TOutput Converter<TInput, TOutput>(TInput input);  public delegate bool    Predicate<T>(T item);
  public delegate bool    Predicate<T>(T item);  }
}   
下面是一些例子   
 int[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 int[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
   int lastOdd = Array.FindLast(list, delegate(int n) { return n % 2 == 1; });
  int lastOdd = Array.FindLast(list, delegate(int n) { return n % 2 == 1; });  // lastOdd is 9
  // lastOdd is 9  
   int[] evens = Array.FindAll(list, delegate(int n) { return n % 2 == 0; });
  int[] evens = Array.FindAll(list, delegate(int n) { return n % 2 == 0; });  // evens contains { 2, 4, 6, 8, 10 }
  // evens contains { 2, 4, 6, 8, 10 }  
   bool hasMultipleOfSeven = Array.Exists(evens, delegate(int n) { return n % 7 == 0; });
  bool hasMultipleOfSeven = Array.Exists(evens, delegate(int n) { return n % 7 == 0; });  // hasMultipleOfSeven is false
  // hasMultipleOfSeven is false  
   Array.Sort(evens, delegate(int a, int b) { return b - a; });
  Array.Sort(evens, delegate(int a, int b) { return b - a; });  // evens is now { 10, 8, 6, 4, 2 } , you can define the sort algorithm by yourself
  // evens is now { 10, 8, 6, 4, 2 } , you can define the sort algorithm by yourself  
   string[] s = Array.ConvertAll<int, string>(evens, delegate(int n) { return "#" + n.ToString(); });
  string[] s = Array.ConvertAll<int, string>(evens, delegate(int n) { return "#" + n.ToString(); });  // s is { "#10", "#8", "#6", "#4", "#2" }  this will be cool if the compiler could do the inferencing for ConvertAll
  // s is { "#10", "#8", "#6", "#4", "#2" }  this will be cool if the compiler could do the inferencing for ConvertAll  
 
pretty easy? isn't it? see much more here. 
 
                    
                     
                    
                 
                    
                

 
   
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号