C#演化类及排序
来源:互联网 发布:知有儿童挑促织的意思 编辑:程序博客网 时间:2024/06/05 23:53
本文主要介绍 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);
}
}
}
- C#演化类及排序
- C#随机数及排序
- C# LINQ需求实现演化
- 架构的演化及模式
- C# GridView 排序及分页
- C# GridView 排序及分页
- C# GridView 排序及分页
- 归并排序(演化版by数组)
- 快速排序(演化版by数组)
- 手机淘宝构架演化实践及优化
- notify 容器类的演化
- C# 自定义类排序
- 插入排序——迈进希尔的演化
- 你知道C#中的Lambda表达式的演化过程吗
- SCA 的演化及与 OSGi 的分析比较
- 大数据(一) - hadoop生态系统及版本演化
- 社会网络中社团发现及网络演化分析
- 社会网络中社团发现及网络演化分析
- Linux - one command per day--rm
- Jquery学习4-3---jquery用法之访问dom元素包含信息。
- pl/sql常用方法
- 设计原则(3)-里氏代换原则
- Java2十大经典中文图书
- C#演化类及排序
- Android之ListActivity(一):布局与数据绑定
- DirectX 图形接口指南
- python 的decorator
- Monkey 工具之使用
- Linux - one command per day--cp
- 【Android】通过软引用实现图片缓存,防止内存溢出
- GTK+主循环(main loop)的工作原理
- 设计原则(4)-依赖倒转原则