首页 技术 正文
技术 2022年11月21日
0 收藏 991 点赞 4,725 浏览 2512 个字

yield 与 IEnumerable<T> 结对出现, 可实现按需获取 , 迭代器模式

static void Main(string[] args)
         {
             try
             {
                 {
                     //Console.WriteLine(“***************Collection**************”);
                     CollectionDemo.Show();
                 }
                 {
                     Console.WriteLine(“*******************Yield********************”);
                     YieldDemo yieldDemo = new YieldDemo();
                     foreach (var item in yieldDemo.Power())
                     {
                         Console.WriteLine(item);//按需获取,要一个拿一个
                         if (item > 100)
                             break;
                     }
                     Console.WriteLine(“*******************************************”);
                     foreach (var item in yieldDemo.Common())
                     {
                         Console.WriteLine(yieldDemo.Common().Count());//先全部获取,然后一起返还
                         if (item > 100)
                             break;
                     }
                 }

{
     List<string> fruits =
     new List<string> { “apple”, “passionfruit”, “banana”, “mango”,
         “orange”, “blueberry”, “grape”, “strawberry” };

IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);
     foreach (var item in query)//遍历才会去查询比较   迭代器 yield
     {

}

IQueryable<string> queryable = fruits.AsQueryable<string>().Where(s => s.Length <6);
     foreach (var item in queryable)//表达式目录树的解析,延迟到遍历的时候才去执行  EF的延迟查询
     {

}

}
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
             }
             Console.Read();
         }

public class YieldDemo

{
     public IEnumerable<int> Power()
     {
         for (int i = 0; i < 10; i++)
         {
             yield return this.Get(i);
             //yield return this.Get(i);

//Console.WriteLine(“这里再来一次”);

//yield return this.Get(i) + 1;
         }
     }

public IEnumerable<int> Common()
     {
         List<int> intList = new List<int>();
         for (int i = 0; i < 10; i++)
         {
             intList.Add(this.Get(i));
         }
         return intList;
     }

private int Get(int num)
     {
         Thread.Sleep(2000);
         return num * DateTime.Now.Second;
     }

}

public static class ExtendMethod

{
     public static IEnumerable<T> ElevenWhere<T>(this IEnumerable<T> source, Func<T, bool> func)
     {
         if (source == null)
         {
             throw new Exception(“source is null”);
         }
         if (func == null)
         {
             throw new Exception(“func is null”);
         }
         foreach (var item in source)
         {
             if (func.Invoke(item))
             {
                 yield return item;
             }
         }
     }

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905