LINQ 的全称是Language-Integrated Query, 在 .NET 2.0 之后就可以使用这种简洁的语法. 使用 LINQ 可以使代码变得简短, 清楚.
比如:
1 2 3 4 5 6 7 8 9 10 11 12 | public class FileCollection : CollectionBase<string> { public override bool Contains(string item) { foreach (string str in this) { if (string.Compare(str, item, true) == 0) return true; } return false; } } |
public class FileCollection : CollectionBase<string> { public override bool Contains(string item) { foreach (string str in this) { if (string.Compare(str, item, true) == 0) return true; } return false; } }
这可以变成 LINQ:
1 2 3 4 5 6 7 | public class FileCollection : CollectionBase<string> { public override bool Contains(string item) { return this.Any(str => string.Compare(str, item, true) == 0); } } |
public class FileCollection : CollectionBase<string> { public override bool Contains(string item) { return this.Any(str => string.Compare(str, item, true) == 0); } }
两种写法并没有什么性能上的区别, 所以LINQ的版本会更好些.
英文: https://helloacm.com/c-example-of-using-linq-1/
GD Star Rating
loading...
本文一共 65 个汉字, 你数一下对不对.loading...
上一篇: 买房记 - Halifax 的 Mortgage Advisor
下一篇: 英国房子的印花税
扫描二维码,分享本文到微信朋友圈
这种文章将是一片沉默?
学习笔记.
呵呵 码农专属讨论范围
LINQ的确是.NET一个很好的特性,不过我一直没系统学过,抽空要专门看一看才行.
不过不懂也可以用,现在通常是写好传统代码,然后让resharper给重构成LINQ…
以前擅长写各种奇葩 LINQ, 后来改写 lambda, 最后还是戒了. 当一个项目里面同事充斥着 C#, LINQ, lamada 的时候, 杯具就来了.
哈哈, 用 Resharper 自动改写成 LINQ