本文主要介绍 C#从2003至2010中类及排序方法的进步
Product.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication2
{
//c#2003
public class Product
{
string name;
public string Name
{
get { return name; }
}
decimal price;
public decimal Price
{
get { return price; }
}
public Product(string name, decimal price)
{
this.name = name;
this.price = price;
}
public static ArrayList GetSampleProducts()
{
ArrayList list = new ArrayList();
list.Add(new Product("West Side Story", 9.99m));
list.Add(new Product("Assassiins", 14.99m));
list.Add(new Product("Frogs", 13.99m));
return list;
}
public override string ToString()
{
return string.Format("{0}:{1}", name, price);
}
}
//c#2005
public class ProductEx
{
string name;
public string Name
{
get { return name; }
private set { name = value; }
}
decimal price;
public decimal Price
{
get { return price; }
private set { price = value; }
}
public ProductEx(string name, decimal price)
{
Name = name;
Price = price;
}
public static List<ProductEx> GetSampleProducts()
{
List<ProductEx> list = new List<ProductEx>();
list.Add(new ProductEx("West Side Story", 9.99m));
list.Add(new ProductEx("Assassiins", 14.99m));
list.Add(new ProductEx("Frogs", 13.99m));
return list;
}
public override string ToString()
{
return string.Format("{0}:{1}", name, price);
}
}
//c#2008
public class ProductExEx
{
public string Name
{
get;
private set;
}
public decimal Price
{
get;
private set;
}
public ProductExEx(string name, decimal price)
{
Name = name;
Price = price;
}
ProductExEx() { }
public static List<ProductExEx> GetSampleProducts()
{
return new List<ProductExEx>
{
new ProductExEx{Name="West Side Story",Price=9.99m},
new ProductExEx{Name="Assassins",Price=14.99m},
new ProductExEx{Name="Frogs",Price=13.99m}
};
}
public override string ToString()
{
return string.Format("{0}:{1}", Name, Price);
}
}
//c#2010
public class ProductExExEx
{
readonly string name;
public string Name
{
get { return name; }
}
readonly decimal price;
public decimal Price
{
get { return price; }
}
public ProductExExEx(string name, decimal price)
{
this.name = name;
this.price = price;
}
public static List<ProductExExEx> GetSampleProducts()
{
return new List<ProductExExEx>
{
new ProductExExEx(name:"West Side Story",price:9.99m),
new ProductExExEx(name:"Assassins",price:14.99m),
new ProductExExEx(name:"Frogs",price:13.99m)
};
}
public override string ToString()
{
return string.Format("{0}:{1}", name, price);
}
}
}
program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("c#2003 sort:");
ArrayList products = Product.GetSampleProducts();
products.Sort(new ProductComparer());
foreach (Product item in products)
{
Console.WriteLine(item);
}
Console.WriteLine("c#2005 sort:");
List<ProductEx> productexs = ProductEx.GetSampleProducts();
productexs.Sort(delegate(ProductEx x,ProductEx y) {return x.Name.CompareTo(y.Name);});
Console.WriteLine("c#2008 sort:");
List<ProductExExEx> list = ProductExExEx.GetSampleProducts();
list.Sort((x, y) => x.Name.CompareTo(y.Name));
foreach (ProductExExEx item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("c#2010 sort:");
foreach (ProductExExEx item in list.OrderBy(p => p.Name))
{
Console.WriteLine(item);
}
}
}
class ProductComparer : IComparer
{
public int Compare(object x, object y)
{
Product first = (Product)x;
Product second = (Product)y;
return first.Name.CompareTo(second.Name);
}
}
class ProductExComparer : IComparer<ProductEx>
{
public int Compare(ProductEx x,ProductEx y)
{
return x.Name.CompareTo(y.Name);
}
}
}