C#语言各个版本特性(三)
2024-08-21 23:43:44
三、查询集合
1.找出List<Product>列表中符合特定条件的所有元素
C#1.1 查询步骤:循环,if判断,打印
product类
using System.Collections;
using System.ComponentModel; namespace Chapter01.CSharp1
{
[Description("Listing 1.01")]
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("Assassins", 14.99m));
list.Add(new Product("Frogs", 13.99m));
list.Add(new Product("Sweeney Todd", 10.99m));
return list;
} public override string ToString()
{
return string.Format("{0}: {1}", name, price);
}
}
}
ArrayListQuery类
using System;
using System.Collections;
using System.ComponentModel; namespace Chapter01.CSharp1
{
[Description("Listing 1.10")]
class ArrayListQuery
{
static void Main()
{
ArrayList products = Product.GetSampleProducts();
foreach (Product product in products)
{
if (product.Price > 10m)
{
Console.WriteLine(product);
}
}
}
}
}
2.测试和打印分开
C#2.0
product类
using System.Collections.Generic;
using System.ComponentModel; namespace Chapter01.CSharp2
{
[Description("Listing 1.02")]
public class Product
{
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 Product(string name, decimal price)
{
Name = name;
Price = price;
} public static List<Product> GetSampleProducts()
{
List<Product> list = new List<Product>();
list.Add(new Product("West Side Story", 9.99m));
list.Add(new Product("Assassins", 14.99m));
list.Add(new Product("Frogs", 13.99m));
list.Add(new Product("Sweeney Todd", 10.99m));
return list;
} public override string ToString()
{
return string.Format("{0}: {1}", name, price);
}
}
}
ListQueryWithDelegates类
using System;
using System.Collections.Generic;
using System.ComponentModel; namespace Chapter01.CSharp2
{
[Description("Listing 1.11")]
class ListQueryWithDelegates
{
static void Main()
{
List<Product> products = Product.GetSampleProducts();
Predicate<Product> test = delegate(Product p)
{ return p.Price > 10m; };
List<Product> matches = products.FindAll(test); Action<Product> print = Console.WriteLine;
matches.ForEach(print);
}
}
}
变量test的初始化使用了匿名方法,而print变量的初始化使用了方法组转换,它简化了从现有方法创建委托的过程。不仅简单而且强大!
ListQueryWithDelegatesCompact类
using System;
using System.Collections.Generic;
using System.ComponentModel; namespace Chapter01.CSharp2
{
[Description("Listing 1.12")]
class ListQueryWithDelegatesCompact
{
static void Main()
{
List<Product> products = Product.GetSampleProducts();
products.FindAll(delegate(Product p) { return p.Price > ; })
.ForEach(delegate(Product p) { Console.WriteLine(p); });
}
}
}
3.用lambda表达式来测试
C#3.0
product
using System.Collections.Generic;
using System.ComponentModel; namespace Chapter01.CSharp3
{
[Description("Listing 1.3")]
class Product
{
public string Name { get; private set; }
public decimal Price { get; private set; } public Product(string name, decimal price)
{
Name = name;
Price = price;
} Product()
{
} public static List<Product> GetSampleProducts()
{
return new List<Product>
{
new Product { Name="West Side Story", Price = 9.99m },
new Product { Name="Assassins", Price=14.99m },
new Product { Name="Frogs", Price=13.99m },
new Product { Name="Sweeney Todd", Price=10.99m}
};
} public override string ToString()
{
return string.Format("{0}: {1}", Name, Price);
}
}
}
ListQueryWithLambdaExpression类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; namespace Chapter01.CSharp3
{
[Description("Listing 1.13")]
class ListQueryWithLambdaExpression
{
static void Main()
{
List<Product> products = Product.GetSampleProducts();
foreach (Product product in products.Where(p => p.Price > ))
{
Console.WriteLine(product);
}
}
}
}
总结:
→C#1,条件和操作紧密耦合两者都是硬编码的
→C#2,条件和操作分开,匿名方法使委托变得简单(匿名方法有助于问题的可分离性)
→C#3Lambda表达式使条件变得更容易阅读(Lambda表达式增强了可读性)。
最新文章
- 选择排序-java
- Python基础:序列(列表、元组)
- .NET破解之谷歌地图下载助手-睿智版
- 一、Oracle分析函数入门
- 远程ubuntu虚拟机Tensorflow搭建 - 1 SSH连接
- RAC 的一些概念性和原理性的知识(转)
- 开源 iOS 项目分类索引大全
- [SQL基础教程] 3-2 对表进行分组
- keystone V3 与Microsoft Active Directory(AD)的集成
- Linux下 Apache Vhost 配置 防止403
- ArcEngine小问题解决
- hydra用法
- Ubuntu18.04安装netstat
- SpringBoot项目部署在同一个tomcat容器报错
- LeetCode(867)
- Java多线程:多线程基础知识
- spring security 实践 + 源码分析
- 弹出序列(python)
- 【微信小程序】——rpx、px、rem等尺寸间关系浅析
- Uva 10559 消除方块