C#开发的进化史
1、数据类型的进化
C#1中实现Product类型代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Product 2 { 3 string name; 4 public string Name 5 { 6 get { return name; } 7 } 8 9 decimal price; 10 public decimal Price 11 { 12 get { return price; } 13 } 14 15 public Product(string name, decimal price) 16 { 17 this.name = name; 18 this.price = price; 19 } 20 21 public static ArrayList GetSampleProducts() 22 { 23 ArrayList list = new ArrayList(); 24 list.Add(new Product("West Side Story", 9.99m)); 25 list.Add(new Product("Assassins", 14.99m)); 26 list.Add(new Product("Frogs", 13.99m)); 27 list.Add(new Product("Sweeney Todd", 10.99m)); 28 return list; 29 30 } 31 32 public override string ToString() 33 { 34 return string.Format("{0}: {1}", name, price); 35 36 } 37 38 39 }
C#2中的强类型集合和私有的赋值方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Product 2 { 3 string name; 4 public string Name 5 { 6 get { return name; } 7 private set { Name = value; } 私有的赋值方法 8 } 9 10 decimal price; 11 public decimal Price 12 { 13 get { return price; } 14 private set { Price = value; } 15 } 16 //C#3:Product(){} 17 public Product(string name, decimal price) 18 { 19 20 Name = name; 21 Price = price; 22 } 23 public static List<Product>GetSampleProducts() 24 { 25 List<Product>list= new List<Product>(); 26 list.Add(new Product("West Side Story", 9.99m)); 27 list.Add(new Product("Assassins", 14.99m)); 28 list.Add(new Product("Frogs", 13.99m)); 29 list.Add(new Product("Sweeney Todd", 10.99m)); 30 return list; 31 32 } 33 34 public override string ToString() 35 { 36 return string.Format("{0}: {1}", name, price); 37 38 } 39 40 }
C#3自动实现的属性和更简单的初始化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Product 2 { 3 public string Name { get; private set; } 4 public string Price { get; private set; } 5 6 public Product(string name, decimal price) 7 { 8 9 Name = name; 10 Price = price; 11 } 12 13 private Product() 14 { 15 } 16 17 public static List<Product> GetSampleProducts() 18 { 19 return new List<Product> 20 { 21 new Product {Name = "West Side Story", Price = 9.99m}, 22 new Product {Name = "Assassins", Price = 14.99m}, 23 new Product {Name = "Frogs", Price = 13.99m}, 24 new Product {Name = "Sweeney Todd", Price = 10.99m} 25 }; 26 } 27 28 public override string ToString() 29 { 30 return string.Format("{0}: {1}", Name, Price); 31 } 32 33 }
C#4中命名实参
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Product 2 { 3 4 public override string ToString() 5 { 6 return string.Format("{0}: {1}", name, price); 7 } 8 //C#4中的参数:尽管私有赋值不能被公共的改变,但如果要求它也不能被私有的改变,将会更加的清晰,在C#4中,我们用调用构造函数时指定实参的名称 9 readonly string name; 10 public string Name { get { return name; } } 11 return new List<Product> 12 { 13 new Product {name = "West Side Story", price = 9.99m}, 14 new Product {name = "Assassins", price = 14.99m}, 15 new Product {name = "Frogs", price = 13.99m}, 16 new Product {name = "Sweeney Todd", price = 10.99m}, 17 }; 18 19 20 }
2、排序和过滤的演化
1、排序
C#1中使用IComparer对ArrayList进行排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class ProductNameComparer : IComparer 2 { 3 public int Compare(object x, object y) 4 { 5 Product first = (Product)x; 6 Product second = (Product)y; 7 return first.Name.CompareTo(second.Name); 8 } 9 } 10 static void Main() 11 { 12 ArrayList products = Product.GetSampleProducts(); 13 products.Sort(new ProductNameComparer()); 14 foreach (Product product in products) 15 { 16 Console.WriteLine(product); 17 } 18 }
C#2中使用IComparer<product>对List<Product>进行排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class ProductNameComparer : IComparer<Product> 2 { 3 public int Compare(Product x, Product y) 4 { 5 return x.Name.CompareTo(y.Name); 6 } 7 } 8 static void Main() 9 { 10 List<Product> products = Product.GetSampleProducts(); 11 products.Sort(new ProductNameComparer()); 12 foreach (Product product in products) 13 { 14 Console.WriteLine(product); 15 } 16 }
C#2中使用委托进行比较
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 * C#2的方法1确实有了一定的改进,但是我们希望能直接指定要进行的标胶,就能开始对产品进行排序,而不需要实现一个接口来做这件事 3 * 下面它告诉sort方法如何用一个委托来比较俩个产品.省略了接口实现的代码和products.Sort(new ProductNameComparer()); 4 * List<Product> products = Product.GetSampleProducts(); 5 * products.Sort(delegate (Product x,Product y) 6 * {return x.Name.CompareTo(y.Name);} 7 * ); 8 */
C#3中使用lambda表达式进行比较
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<Product> products = Product.GetSampleProducts(); 2 products.Sort((x,y)=>x.Name.CompareTo(y.Name)); 3 foreach(Product product in products) 4 { 5 console.writeline(product); 6 } 7 C#3还有另一种写法排序 8 foreach(Product product in product.OrderBy(p=>p.Name)) 9 { 10 console.writeline(product);//通知轻松的按顺序打印名称,同时不必修改原产品列表 11 }
2、查询
循环、测试和打印(C#1)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ArrayList products = Product.GetSampleProducts(); 2 foreach (Product product in products.Cast<Product>().Where(product => product.Price > 10m)) 3 { 4 Console.WriteLine(product); 5 }
测试和打印分开进行(C#2)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<Product> products =Product.GetSampleProducts(); 2 Predicate<Product> test = delegate(Product p){ return p.Price > 10m;}; 3 List<Product> matches=products.FindAll(test); 4 Action<Product> print=console.writeLine; 5 matches.ForEach(print);
测试和打印分开进行的另一个版本(C#2)
List<Product> products =Product.GetSampleProducts(); products.FindAll(delegate (Product p){ return p.Price >10;}).ForEach(Console.WriteLine);
用lambda表达式来进行测试(c#3)
List<Product> products =Product.GetSampleProducts(); foreach(Product product in products.Where(p=>p.price>10)) { Console.WriteLine(product); }
C#中使用Linq表达式
foreach (Product product in products.Cast<Product>().Where(product => product.Price > 10m)) { Console.WriteLine(product); }