welcome to Qijie's Blog 薛其杰

 

代码
 1  /// <summary>
 2     /// New Feature in .NET 4.0
 3     /// Compare two array
 4     /// using IStructuralEquatable
 5     /// using EqualityComparer<T>
 6     /// using Comparer<T>
 7     /// </summary>
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int[] arr1 = new int[]{
13                 1,2,3,5
14             };
15             int[] arr2 = new int[]{
16                 1,2,3,4
17             };
18 
19             bool isEqual = (arr1 as IStructuralEquatable).Equals (
20                 arr2, EqualityComparer <int>.Default );
21 
22             int bigger = (arr1 as IStructuralComparable).CompareTo(
23                 arr2, Comparer<int>.Default);
24 
25             Console.WriteLine(String.Format ("arr1==arr2 :{0}",isEqual));
26             Console.WriteLine(String.Format("arr1-arr3: {0}",bigger));
27 
28             Console.WriteLine("");
29 
30             string[] str1 = new string[]{
31                 "a","cb"
32             };
33             string[] str2 = new string[]{
34                 "a","c"
35             };
36 
37             // if str1=str2 then sbigger=0 ; if str1>str2 then sbigger=1; if str1<str2 then sbigger=-1
38             int sbigger = (str1 as IStructuralComparable).CompareTo(
39                 str2, Comparer<string>.Default);
40             Console.WriteLine(String.Format ("str1-str2:{0}",sbigger));            
41         }
42     }
Output:
 
 

 

 

posted on 2010-01-04 18:01  零点零一  阅读(371)  评论(0编辑  收藏  举报