C# Enumerable yield and foreach

The professor today asks us in the class to solve this exercise in C# can someone help me out i'm lost...

The purpose of this exercise and the next one is to emphasize the power of enumerables and the yield and foreach statements. Declare a generic static method Flatten that takes as argument an array of IEnumerable<T> and returns an IEnumerable<T> . Use foreach statements and the yield return statement. The method should have this header:

public static IEnumerable<T> Flatten<T>(IEnumerable<T>[] ebles) { ... }

If you call the method as shown below, you should get 2 3 5 7 2 3 5 7 2 3 5 7:

IEnumerable<int>[] ebles = new IEnumerable<int>[3];
ebles[0] = ebles[1] = ebles[2] = new int[] { 2, 3, 5, 7 };
foreach (int i in Flatten<int>(ebles))
Console.Write(i + " ");

You need two nested foreach loops. One iterates the ebles the inner one the elements in each list. The innermost loop contains a yield return element ;

This is the outline. Now go and read about each of the words mentioned in this outline.

链接地址: http://www.djcxy.com/p/53714.html

上一篇: 删除后mysql插入失败,因为“重复条目”

下一篇: C#枚举yield和foreach