Linq101-CustomSequence

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 
 5 namespace Linq101
 6 {
 7     class CustomSequence
 8     {
 9         public void Linq98()
10         {
11             int[] vectorA = { 0, 2, 4, 5, 6 };
12             int[] vectorB = { 1, 3, 5, 7, 8 };
13 
14             int result = vectorA.Combine(vectorB, (a, b) => a * b).Sum();
15             Console.WriteLine(result);
16         }
17     }
18 
19     public static class CustomSequenceOperators
20     {
21         public static IEnumerable<int> Combine(this IEnumerable<int> first, IEnumerable<int> second, Func<int, int, int> func)
22         {
23             //List<int> list=new List<int>();
24             using (IEnumerator<int> e1 = first.GetEnumerator(), e2 = second.GetEnumerator())
25             {
26                 while (e1.MoveNext() && e2.MoveNext())
27                 {
28                     yield return func(e1.Current, e2.Current);
29                     //list.Add(func(e1.Current, e2.Current));
30                 }
31             }
32             //return list;
33         }
34     }
35 }

 

posted @ 2015-05-12 17:38  David Huang  阅读(239)  评论(0编辑  收藏  举报