摘要:
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 }; //为. 阅读全文
摘要:
隐式类型局部变量 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 阅读全文
摘要:
假设给我们一个泛型对象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> 阅读全文