摘要:
Linq查询运算符设计用于任何实现了IEnumerable<T>接口的类型。但System.Collections中传统的非泛型容器类未实现IEnumerable<T>接口,在这些非泛型容器上怎样执行Linq查询呢? 我们可以使用扩展方法Enumerable.OfType<T>()扩展方法将非泛型容器转换为IEnumerable<T>类型后,再执行查询。Enumerable.OfType<T>扩展方法的原型为: public static IEnumerable<T> OfType<T>(this IEnume 阅读全文
posted @ 2011-04-05 21:34
辛勤的代码工
阅读(444)
评论(0)
推荐(0)
摘要:
Linq查询表达式在我们迭代内容前,不会真正进行运算,这叫做延迟执行。这种方式的好处是可以为相同的容器多次应用相同的Linq查询,而始终获得最新的结果。 示例代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestVar{ class Program { //Linq查询的延迟执行示例 static void Main(string[] args) { int[] intAry = new int[] { 1, 2, 3, 4, 5, 6 }; //为. 阅读全文
posted @ 2011-04-05 21:04
辛勤的代码工
阅读(513)
评论(0)
推荐(0)
摘要:
隐式类型局部变量 C#3.0提供了一个新的关键字var,使用该关键字,编译器会根据用于初始化局部变量的初始值推断出变量的数据类型。 示例代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestVar{ class Program { static void Main(string[] args) { //定义int数组 int[] intAry = new int[] { 1, 2, 3, 4, 5, 6 }; //var varSet = intAry 阅读全文
posted @ 2011-04-05 18:31
辛勤的代码工
阅读(480)
评论(0)
推荐(0)
摘要:
假设给我们一个泛型对象List<T>,T为int类型,要求我们使用该对象方法FindAll(Predicate<T> match)从中找出该List中的偶数,您如何实现? 说明一下:Predicate<T>是一个泛型委托,它的原型为public delegate bool Predicate<T>(T obj),该委托传入一个T类型对象,经逻辑判断后返回布尔值。委托 可能您首先想到的是用委托实现,实现方法如下: //方法1 static void Method1() { //创建List<int>对象 List<int> 阅读全文
posted @ 2011-04-05 14:38
辛勤的代码工
阅读(616)
评论(1)
推荐(0)
浙公网安备 33010602011771号