码迷,mamicode.com
首页 > 其他好文 > 详细

使用 yield return 返回筛选出来的集合

时间:2018-11-10 16:41:40      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:for   ==   write   new   col   ram   解决   oid   ring   

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             string[] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" };
 6 
 7             //使用yield ruturn的解决方案。
 8             var namesFive = GetFiveName(names);
 9 
10             foreach (var n in namesFive)
11             {
12                 Console.WriteLine(n);
13             }
14 
15 
16             foreach (string item in namesFive)
17             {
18                 Console.WriteLine(item);
19             }
20 
21         }
22 
23         //老式的做法
24         //private static IEnumerable GetFiveName(string[] ts)
25         //{
26         //    List<string> fiveNames = new List<string>();
27         //    foreach (var item in ts)
28         //    {
29         //        if (item.Length == 5)
30         //        {
31         //            fiveNames.Add(item);
32         //            //yield return item;
33         //        }
34         //    }
35         //    return fiveNames;
36         //}
37 
38         private static IEnumerable GetFiveName(string[] ts)
39         {
40             foreach (var item in ts)
41             {
42                 if (item.Length == 5)
43                 {
44                     yield return item;
45                 }
46             }
47         }
48     }

 

使用 yield return 返回筛选出来的集合

标签:for   ==   write   new   col   ram   解决   oid   ring   

原文地址:https://www.cnblogs.com/myBlogOu/p/9939595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!