日期:2014-05-20 浏览次数:20986 次
static class Program { static void Main(string[] args) { LinqTest(); } public static void LinqTest() { int[] vectorA = { 0, 2, 4, 5, 6 }; int[] vectorB = { 1, 3, 5, 7, 8 }; foreach (var num in vectorA.Combine(vectorB, (a, b) => a * b)) { Console.WriteLine("Dot product: {0}", num); } Console.ReadLine(); } public static IEnumerable<int> Combine(this IEnumerable<int> first, IEnumerable<int> second, Func<int, int, int> func) { var e1 = first.GetEnumerator(); var e2 = second.GetEnumerator(); while (e1.MoveNext() && e2.MoveNext()) yield return func(Convert.ToInt32(e1.Current), Convert.ToInt32(e2.Current)); } }