Enumerable in C#——内迭代与外迭代示例

昨晚家里上不了网,打开以前下的视频:what's new in csharp 3.0 ?.感叹于VS开发环境的中Code Snippet,以及 CSharp3.0 对迭代的支持。

随手仿照视频写了一个代码,用来演示CSharp3.0对迭代的支持。

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Linq;
  4 using  System.Text;
  5
  6 namespace  EnumerableTest
  7 ExpandedBlockStart.gifContractedBlock.gif {
  8    class Customer
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        public string CustomerID getset; }
 11ExpandedSubBlockStart.gifContractedSubBlock.gif        public string ContactName getset; }
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        public string City getset; }
 13    }

 14
 15
 16    class Program
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 18        static void Main(string[] args)
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 20            List<Customer> customers = LoadCustomers();
 21
 22ContractedSubBlock.gifExpandedSubBlockStart.gif            Outer Iteration#region Outer Iteration
 23            //使用外迭代 One
 24            foreach (var cust in customers)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 26                PrintCustomer(cust);  //可将迭代子(Iterator)传给其它函数
 27
 28                //也可以进行Filter,但不是很Elegant
 29                if (cust.City == "Suzhou")
 30                    PrintCustomer(cust);
 31            }

 32            SeparatorLine();
 33            //使用外迭代 Two
 34            IEnumerator<Customer> itor = customers.GetEnumerator();
 35            while (itor.MoveNext())
 36ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 37                PrintCustomer(itor.Current);
 38            }

 39            SeparatorLine();
 40            
 41            #endregion

 42
 43
 44ContractedSubBlock.gifExpandedSubBlockStart.gif            Inner Iteration#region Inner Iteration
 45            //使用内迭代,对每个元素执行一个操作(函数),思想上实现了函数式编程
 46            //语义上:对每个元素执行,一个函数Action(PrintCustomer),下面的语义等同于:
 47            //Action<Customer> printCustomer = delegate(Customer c)
 48            //{
 49            //    Console.WriteLine("ID:{0} Contact:{1} City:{2}", c.CustomerID.PadRight(10), c.ContactName.PadRight(10), c.City);
 50            //};
 51            //customers.ForEach(printCustomer);
 52            customers.ForEach(c => PrintCustomer(c));  //More convenience, more elegant
 53            SeparatorLine();
 54
 55            //使用内迭代,进行Filter:Where(c => c.City == "Suzhou")
 56            customers.Where(c => c.City == "Suzhou").ToList().ForEach(c => PrintCustomer(c));
 57            SeparatorLine();
 58            //多级Filter
 59            customers.Where(c => c.City == "Suzhou").Where(c => c.ContactName == "Peter").ToList().ForEach(c => PrintCustomer(c));
 60            SeparatorLine(); 
 61            #endregion

 62
 63
 64ContractedSubBlock.gifExpandedSubBlockStart.gif            LINQ,对迭代的SQL Syntax式支持,我不太喜欢,还是感觉函数式编程更爽些,更直观些#region LINQ,对迭代的SQL Syntax式支持,我不太喜欢,还是感觉函数式编程更爽些,更直观些
 65            //似乎对于LINQ来说,Query功能完善。而:对于每一个元素执行一个操作(函数)却不支持.
 66            var result = from c in customers
 67                         where c.City == "Suzhou"
 68                         where c.ContactName == "Peter"
 69                         select c;
 70
 71            result.ToList().ForEach(c => PrintCustomer(c));
 72
 73            SeparatorLine();
 74            #endregion

 75
 76
 77            Console.ReadKey();
 78        }

 79
 80        private static void SeparatorLine()
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 82            Console.WriteLine();
 83        }

 84
 85        private static void PrintCustomer(Customer cust)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 87            Console.WriteLine("ID:{0} Contact:{1} City:{2}", cust.CustomerID.PadRight(10), cust.ContactName.PadRight(10), cust.City);
 88        }

 89
 90        private static List<Customer> LoadCustomers()
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 92            List<Customer> customers = new List<Customer>() 
 93ExpandedSubBlockStart.gifContractedSubBlock.gif            
 94ExpandedSubBlockStart.gifContractedSubBlock.gif                new Customer(){ CustomerID="Andrew", ContactName="Anco", City="Hefei"},
 95ExpandedSubBlockStart.gifContractedSubBlock.gif                new Customer(){ CustomerID="Nortel", ContactName="Anco", City="Shanghai"},
 96ExpandedSubBlockStart.gifContractedSubBlock.gif                new Customer(){ CustomerID="Cisco", ContactName="Candy", City="Beijing"},
 97ExpandedSubBlockStart.gifContractedSubBlock.gif                new Customer(){ CustomerID="HP", ContactName="Peter", City="Suzhou"},
 98ExpandedSubBlockStart.gifContractedSubBlock.gif                new Customer(){ CustomerID="Lenovo", ContactName="Linda", City="Suzhou"}
 99            }
;
100            return customers;
101        }

102    }

103}

104

 

转载于:https://www.cnblogs.com/Jerry-Chou/archive/2009/11/03/enumerable-in-csharp3.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值