三、查詢集合 1.找出List<Product>列表中符合特定條件的所有元素 C#1.1 查詢步驟:迴圈,if判斷,列印 product類 1 using System.Collections; 2 using System.ComponentModel; 3 4 namespace Chapter ...
三、查詢集合
1.找出List<Product>列表中符合特定條件的所有元素
C#1.1 查詢步驟:迴圈,if判斷,列印
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 }View Code
ArrayListQuery類
1 using System; 2 using System.Collections; 3 using System.ComponentModel; 4 5 namespace Chapter01.CSharp1 6 { 7 [Description("Listing 1.10")] 8 class ArrayListQuery 9 { 10 static void Main() 11 { 12 ArrayList products = Product.GetSampleProducts(); 13 foreach (Product product in products) 14 { 15 if (product.Price > 10m) 16 { 17 Console.WriteLine(product); 18 } 19 } 20 } 21 } 22 }View Code
2.測試和列印分開
C#2.0
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 }View Code
ListQueryWithDelegates類
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 5 namespace Chapter01.CSharp2 6 { 7 [Description("Listing 1.11")] 8 class ListQueryWithDelegates 9 { 10 static void Main() 11 { 12 List<Product> products = Product.GetSampleProducts(); 13 Predicate<Product> test = delegate(Product p) 14 { return p.Price > 10m; }; 15 List<Product> matches = products.FindAll(test); 16 17 Action<Product> print = Console.WriteLine; 18 matches.ForEach(print); 19 } 20 } 21 }View Code
變數test的初始化使用了匿名方法,而print變數的初始化使用了方法組轉換,它簡化了從現有方法創建委托的過程。不僅簡單而且強大!
ListQueryWithDelegatesCompact類
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 5 namespace Chapter01.CSharp2 6 { 7 [Description("Listing 1.12")] 8 class ListQueryWithDelegatesCompact 9 { 10 static void Main() 11 { 12 List<Product> products = Product.GetSampleProducts(); 13 products.FindAll(delegate(Product p) { return p.Price > 10; }) 14 .ForEach(delegate(Product p) { Console.WriteLine(p); }); 15 } 16 } 17 }View Code
3.用lambda表達式來測試
C#3.0
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 }View Code
ListQueryWithLambdaExpression類
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.13")] 9 class ListQueryWithLambdaExpression 10 { 11 static void Main() 12 { 13 List<Product> products = Product.GetSampleProducts(); 14 foreach (Product product in products.Where(p => p.Price > 10)) 15 { 16 Console.WriteLine(product); 17 } 18 } 19 } 20 }View Code
總結:
→C#1,條件和操作緊密耦合兩者都是硬編碼的
→C#2,條件和操作分開,匿名方法使委托變得簡單(匿名方法有助於問題的可分離性)
→C#3Lambda表達式使條件變得更容易閱讀(Lambda表達式增強了可讀性)。