欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#语言各个版本特性(三)

程序员文章站 2022-05-06 12:41:24
三、查询集合 1.找出List列表中符合特定条件的所有元素 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类

C#语言各个版本特性(三)
 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类

C#语言各个版本特性(三)
 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类

C#语言各个版本特性(三)
 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类

C#语言各个版本特性(三)
 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类

C#语言各个版本特性(三)
 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

C#语言各个版本特性(三)
 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类

C#语言各个版本特性(三)
 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表达式增强了可读性)。