C#语言各个版本特性(三)
三、查询集合
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表达式增强了可读性)。
C#语言各个版本特性(三)的更多相关文章
- 为什么说JAVA中要慎重使用继承 C# 语言历史版本特性(C# 1.0到C# 8.0汇总) SQL Server事务 事务日志 SQL Server 锁详解 软件架构之 23种设计模式 Oracle与Sqlserver:Order by NULL值介绍 asp.net MVC漏油配置总结
为什么说JAVA中要慎重使用继承 这篇文章的主题并非鼓励不使用继承,而是仅从使用继承带来的问题出发,讨论继承机制不太好的地方,从而在使用时慎重选择,避开可能遇到的坑. JAVA中使用到继承就会有两 ...
- C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新) C#各版本新特性 C#版本和.NET版本以及VS版本的对应关系
C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新) 2017年08月06日 11:53:13 阅读数:6705 历史版本 C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有1 ...
- C# 语言历史版本特性(C# 1.0到C# 8.0汇总)
历史版本 C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有17年的历史,语言本身具有丰富的特性,微软对其更新支持也十分支持.微软将C#提交给标准组织ECMA,C# 5.0目前是ECM ...
- C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新)
历史版本C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有17年的历史,语言本身具有丰富的特性,微软对其更新支持也十分支持.微软将C#提交给标准组织ECMA,C# 5.0目前是ECMA ...
- [转]C# 语言历史版本特性(C# 1.0到C# 8.0汇总)
历史版本 C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有17年的历史,语言本身具有丰富的特性,微软对其更新支持也十分支持.微软将C#提交给标准组织ECMA,C# 5.0目前是ECM ...
- C#语言各个版本特性(一)
一.c#版本中添加的功能: C#2.0 泛型 部分类型 匿名方法 迭代器 可空类型 Getter / setter单独可访问性 方法组转换(代表) Co- and Contra-variance fo ...
- C#语言各个版本特性(二)
二.排序Product 1.按名称对产品进行排序,以特定顺序显示一个列表的最简单方式就是先将列表排序,再遍历并显示其中的项. C#1.1 使用IComparer对ArrayList进行排序 produ ...
- Python 如何移除旧的版本特性,如何迎接新的特性?
2020 年 4 月 20 日,Python 2 的最后一个版本 2.7.18 发布了,这意味着 Python 2 是真正的 EOL(end of life)了,一个时代终于落幕了. Python 2 ...
- Java14版本特性【一文了解】
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
随机推荐
- ORA-12521: TNS: 监听程序当前无法识别连接描述符中请求的实例(原)
今天登录PL/SQL出现问题: ---------------------------sys@RAC1 as SYSDBA---------------------------ORA-12521: T ...
- LoadXml载入Xhtml文件速度很慢
如果有如下的Xhtml文字,在.Net中用XmlDocument.LoadXml载入的时候,速度很慢. <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...
- SpringBoot入门篇--对于JSON数据的返回以及处理一
在后台的开发过程中不可避免的就是一系列对JSON数据的返回,需要我们进行的就是提供各种各样的数据.一般情况下数据类型最常用的就是JSON以及XML,在这里我们就讲讲在SpringBoot里面我们怎样进 ...
- django2.0实现数据详情页展示的流程
思路整理 1 先在urls.py中,定义路由获取的格式 url(r'^detail/(\d+)/$', views.blog_detail), 2 然后在views.py,定义数据获取的方法 def ...
- windows 下 YII2 配置 memcache
环境: 操作系统 :Windows 7; php: 5.6.8 apche:2.4.12 1.首先安装PHP memcache 拓展,安装方法如下: 1.1下载 memcache 拓展DLL: ht ...
- c#,条码
static void Generate2(string text){ BarcodeWriter writer = new BarcodeWriter(); //使用ITF 格式,不能被现在常用的支 ...
- 决策树与树集成模型(bootstrap, 决策树(信息熵,信息增益, 信息增益率, 基尼系数),回归树, Bagging, 随机森林, Boosting, Adaboost, GBDT, XGboost)
1.bootstrap 在原始数据的范围内作有放回的再抽样M个, 样本容量仍为n,原始数据中每个观察单位每次被抽到的概率相等, 为1/n , 所得样本称为Bootstrap样本.于是可得到参数θ的 ...
- OGNL入门
------------------siwuxie095 OGNL 入门 1.OGNL 支持对象方法调用,即 objName.methodName() 如:使用 OGNL 表达式 + Struts2 ...
- win10 跳过max path 260限制
参考: https://www.howtogeek.com/266621/how-to-make-windows-10-accept-file-paths-over-260-characters/ 注 ...
- Android使用ListView使用方法
Android使用ListView应该注意的地方 在ListView中设置Selector为null会报空指针? mListView.setSelector(null);//空指针 试试下面这种: ...