当前位置: 首页>>代码示例>>C#>>正文


C# Comparison<T>代理代码示例

本文整理汇总了C#中System.Comparison<T>代理的典型用法代码示例。如果您正苦于以下问题:C# Comparison<T>代理的具体用法?C# Comparison<T>怎么用?C# Comparison<T>使用的例子?那么恭喜您, 这里精选的代理代码示例或许可以为您提供帮助。


Comparison<T>代理属于System命名空间,在下文中一共展示了Comparison<T>代理的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CompareDinosByLength

//引入命名空间
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()
    {
        List<string> dinosaurs = new List<string>();
        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<string> delegate:");
        dinosaurs.Sort(CompareDinosByLength);
        Display(dinosaurs);
    }

    private static void Display(List<string> list)
    {
        Console.WriteLine();
        foreach( string s in list )
        {
            if (s == null)
                Console.WriteLine("(null)");
            else
                Console.WriteLine("\"{0}\"", s);
        }
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:85,代码来源:Comparison

输出:

"Pachycephalosaurus"
"Amargasaurus"
""
(null)
"Mamenchisaurus"
"Deinonychus"

Sort with generic Comparison delegate:

(null)
""
"Deinonychus"
"Amargasaurus"
"Mamenchisaurus"
"Pachycephalosaurus"

示例2: CityInfo

//引入命名空间
using System;

public class CityInfo
{
   string cityName;
   string countryName;
   int pop2010;
   
   public CityInfo(string name, string country, int pop2010)
   {
      this.cityName = name;
      this.countryName = country;
      this.pop2010 = pop2010;
   }
   
   public string City
   { get { return this.cityName; } } 
   
   public string Country
   { get { return this.countryName; } }

   public int Population
   { get { return this.pop2010; } }
   
   public static int CompareByName(CityInfo city1, CityInfo city2)
   { 
      return String.Compare(city1.City, city2.City);
   }
   
   public static int CompareByPopulation(CityInfo city1, CityInfo city2)
   {
      return city1.Population.CompareTo(city2.Population);
   }
   
   public static int CompareByNames(CityInfo city1, CityInfo city2)
   {
      return String.Compare(city1.Country + city1.City, city2.Country + city2.City);
   }      
}

public class Example
{
   public static void Main()
   {
      CityInfo NYC = new CityInfo("New York City", "United States of America", 8175133 );
      CityInfo Det = new CityInfo("Detroit", "United States of America", 713777);
      CityInfo Paris = new CityInfo("Paris", "France",  2193031);
      CityInfo[] cities = { NYC, Det, Paris };
      // Display ordered array.
      DisplayArray(cities);
      
      // Sort array by city name.
      Array.Sort(cities, CityInfo.CompareByName);
      DisplayArray(cities);
      
      // Sort array by population.
      Array.Sort(cities, CityInfo.CompareByPopulation);
      DisplayArray(cities);
      
      // Sort array by country + city name.
      Array.Sort(cities, CityInfo.CompareByNames);
      DisplayArray(cities);
   }
   
   private static void DisplayArray(CityInfo[] cities)
   {
      Console.WriteLine("{0,-20} {1,-25} {2,10}", "City", "Country", "Population");
      foreach (var city in cities)
         Console.WriteLine("{0,-20} {1,-25} {2,10:N0}", city.City, 
                           city.Country, city.Population);

      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:75,代码来源:Comparison

输出:

City                 Country                   Population
New York City        United States of America   8,175,133
Detroit              United States of America     713,777
Paris                France                     2,193,031

City                 Country                   Population
Detroit              United States of America     713,777
New York City        United States of America   8,175,133
Paris                France                     2,193,031

City                 Country                   Population
Detroit              United States of America     713,777
Paris                France                     2,193,031
New York City        United States of America   8,175,133

City                 Country                   Population
Paris                France                     2,193,031
Detroit              United States of America     713,777
New York City        United States of America   8,175,133


注:本文中的System.Comparison<T>代理示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。