System.Comparison 委托
方法描述
表示比较同一类型的两个对象的方法。
语法定义(C# System.Comparison 委托 的用法)
public delegate int Comparison( T x, T y )
提示和注释
此委托由 Array 类的 Sort
System.Comparison 委托例子
显示该列表,使用表示 CompareDinosByLength 方法的 Comparison
using System; using System.Collections.Generic; public class Example { private static int CompareDinosByLength(string x, string y) { if (x == null) { if (y == null) { // If x is null and y is null, they're // equal. return 0; } else { // If x is null and y is not null, y // is greater. return -1; } } else { // If x is not null... // if (y == null) // ...and y is null, x is greater. { return 1; } else { // ...and y is not null, compare the // lengths of the two strings. // int retval = x.Length.CompareTo(y.Length); if (retval != 0) { // If the strings are not of equal length, // the longer string is greater. // return retval; } else { // If the strings are of equal length, // sort them with ordinary string comparison. // return x.CompareTo(y); } } } } public static void Main() { Listdinosaurs = new List (); dinosaurs.Add("Pachycephalosaurus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add(""); dinosaurs.Add(null); dinosaurs.Add("Mamenchisaurus"); dinosaurs.Add("Deinonychus"); Display(dinosaurs); Console.WriteLine("\nSort with generic Comparison delegate:"); dinosaurs.Sort(CompareDinosByLength); Display(dinosaurs); } private static void Display(List list) { Console.WriteLine(); foreach( string s in list ) { if (s == null) Console.WriteLine("(null)"); else Console.WriteLine("\"{0}\"", s); } } } /* This code example produces the following output: "Pachycephalosaurus" "Amargasaurus" "" (null) "Mamenchisaurus" "Deinonychus" Sort with generic Comparison delegate: (null) "" "Deinonychus" "Amargasaurus" "Mamenchisaurus" "Pachycephalosaurus" */
继承层次结构
线程安全
版本信息
.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1
适用平台
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。