VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之C#语言各个版本特性

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

二、排序Product

1.按名称对产品进行排序,以特定顺序显示一个列表的最简单方式就是先将列表排序,再遍历并显示其中的项。

C#1.1 使用IComparer对ArrayList进行排序

product类

复制代码
 1 using System.Collections;
 2 using System.ComponentModel;
 3 
 4 namespace Chapter01.CSharp1
 5 {
 6     [Description("Listing 1.01")]
 7     public class Product
 8     {
 9         string name;
10         public string Name
11         {
12             get { return name; }
13         }
14 
15         decimal price;
16         public decimal Price
17         {
18             get { return price; }
19         }
20 
21         public Product(string name, decimal price)
22         {
23             this.name = name;
24             this.price = price;
25         }
26 
27         public static ArrayList GetSampleProducts()
28         {
29             ArrayList list = new ArrayList();
30             list.Add(new Product("West Side Story", 9.99m));
31             list.Add(new Product("Assassins", 14.99m));
32             list.Add(new Product("Frogs", 13.99m));
33             list.Add(new Product("Sweeney Todd", 10.99m));
34             return list;
35         }
36 
37         public override string ToString()
38         {
39             return string.Format("{0}: {1}", name, price);
40         }
41     }
42 }
复制代码

ArrayListSort类

复制代码
 1 using System;
 2 using System.Collections;
 3 using System.ComponentModel;
 4 
 5 namespace Chapter01.CSharp1
 6 {
 7     [Description("Listing 1.05")]
 8     class ArrayListSort
 9     {
10         class ProductNameComparer : IComparer
11         {
12             public int Compare(object x, object y)
13             {
14                 Product first = (Product)x;
15                 Product second = (Product)y;
16                 return first.Name.CompareTo(second.Name);
17             }
18         }
19 
20         static void Main()
21         {
22             ArrayList products = Product.GetSampleProducts();
23             products.Sort(new ProductNameComparer());
24             foreach (Product product in products)
25             {
26                 Console.WriteLine(product);
27             }
28         }
29     }
30 }
复制代码

提供一个IComparer实现,比较器。或者Product类实现IComparable。

区别:IComparer和IComparable(比较器接口和可比较的接口)

缺陷:Compare方法中显示强制类型转换,而ArrayList是类型不安全的,转换为对象Product时可能报错。foreach循环中隐式的编译器自动类型转换,转换为Product类型执行时可能报错。

2.C#2.0 引入泛型,使用IComparer<Product>对List<Product>进行排序

product类

复制代码
 1 using System.Collections.Generic;
 2 using System.ComponentModel;
 3 
 4 namespace Chapter01.CSharp2
 5 {
 6     [Description("Listing 1.02")]
 7     public class Product
 8     {
 9         string name;
10         public string Name
11         {
12             get { return name; }
13             private set { name = value; }
14         }
15 
16         decimal price;
17         public decimal Price
18         {
19             get { return price; }
20             private set { price = value; }
21         }
22 
23         public Product(string name, decimal price)
24         {
25             Name = name;
26             Price = price;
27         }
28 
29         public static List<Product> GetSampleProducts()
30         {
31             List<Product> list = new List<Product>();
32             list.Add(new Product("West Side Story", 9.99m));
33             list.Add(new Product("Assassins", 14.99m));
34             list.Add(new Product("Frogs", 13.99m));
35             list.Add(new Product("Sweeney Todd", 10.99m));
36             return list;
37         }
38 
39         public override string ToString()
40         {
41             return string.Format("{0}: {1}", name, price);
42         }
43     }
44 }
复制代码

ListSortWithComparer类

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 
 5 namespace Chapter01.CSharp2
 6 {
 7     [Description("Listing 1.06")]
 8     class ListSortWithComparer
 9     {
10         class ProductNameComparer : IComparer<Product>
11         {
12             public int Compare(Product first, Product second)
13             {
14                 return first.Name.CompareTo(second.Name);
15             }
16         }
17 
18         static void Main()
19         {
20             List<Product> products = Product.GetSampleProducts();
21             products.Sort(new ProductNameComparer());
22             foreach (Product product in products)
23             {
24                 Console.WriteLine(product);
25             }
26         }
27     }
28 }
复制代码

使用Comparison<Product>对List<Product>进行排序(C#2),不需要实现ProductNameComparer比较器类型,只是创建一个委托实例(C#2.0 匿名方法)。

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 
 5 namespace Chapter01.CSharp2
 6 {
 7     [Description("Listing 1.07")]
 8     class ListSortWithComparisonDelegate
 9     {
10         static void Main()
11         {
12             List<Product> products = Product.GetSampleProducts();
13             products.Sort(delegate(Product first, Product second)
14                 { return first.Name.CompareTo(second.Name); }
15             );
16             foreach (Product product in products)
17             {
18                 Console.WriteLine(product);
19             }
20         }
21     }
22 }
复制代码

3.C#3.0 Lambda表达式 在Lambda表达式中使用Comparison<Product>进行排序(C#3)

product类

复制代码
 1 using System.Collections.Generic;
 2 using System.ComponentModel;
 3 
 4 namespace Chapter01.CSharp3
 5 {
 6     [Description("Listing 1.3")]
 7     class Product
 8     {
 9         public string Name { get; private set; }
10         public decimal Price { get; private set; }
11 
12         public Product(string name, decimal price)
13         {
14             Name = name;
15             Price = price;
16         }
17 
18         Product()
19         {
20         }
21 
22         public static List<Product> GetSampleProducts()
23         {
24             return new List<Product>
25             {
26                 new Product { Name="West Side Story", Price = 9.99m },
27                 new Product { Name="Assassins", Price=14.99m },
28                 new Product { Name="Frogs", Price=13.99m },
29                 new Product { Name="Sweeney Todd", Price=10.99m}
30             };
31         }
32 
33         public override string ToString()
34         {
35             return string.Format("{0}: {1}", Name, Price);
36         }
37     }
38 }
复制代码

ListSortWithLambdaExpression类

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 
 5 namespace Chapter01.CSharp3
 6 {
 7     [Description("Listing 1.08")]
 8     class ListSortWithLambdaExpression
 9     {
10         static void Main()
11         {
12             List<Product> products = Product.GetSampleProducts();
13             products.Sort(
14                 (first, second) => first.Name.CompareTo(second.Name)
15             );
16             foreach (Product product in products)
17             {
18                 Console.WriteLine(product);
19             }
20         }
21     }
22 }
复制代码

ListOrderWithExtensionMethod类 使用扩展方法对List<Product>进行排序

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Linq;
 5 
 6 namespace Chapter01.CSharp3
 7 {
 8     [Description("Listing 1.09")]
 9     class ListOrderWithExtensionMethod
10     {
11         static void Main()
12         {
13             List<Product> products = Product.GetSampleProducts();
14 
15             foreach (Product product in products.OrderBy(p => p.Name))
16             {
17                 Console.WriteLine(product);
18             }
19         }
20     }
21 }
复制代码

总结:

→C#1,弱类型的比较功能不支持委托排序。

→C#2,强类型的比较功能,委托比较,匿名方法。

→C#3Lambda表达式,扩展方法,允许列表保持未排序状态。

相关教程