foreach should return the same enumerable
This commit is contained in:
@@ -111,18 +111,19 @@ namespace System.Linq
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
||||||
{
|
{
|
||||||
Enumerate(source, action);
|
return Enumerate(source, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
|
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
|
||||||
{
|
{
|
||||||
var index = 0;
|
var index = 0;
|
||||||
var enumerator = source.GetEnumerator();
|
var enumerator = source.GetEnumerator();
|
||||||
while(enumerator.MoveNext())
|
while(enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
action?.Invoke(enumerator.Current, index);
|
action?.Invoke(enumerator.Current, index);
|
||||||
|
yield return enumerator.Current;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,12 +142,13 @@ namespace System.Linq
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Enumerate<T>(IEnumerable<T> source, Action<T> action)
|
private static IEnumerable<T> Enumerate<T>(IEnumerable<T> source, Action<T> action)
|
||||||
{
|
{
|
||||||
var enumerator = source.GetEnumerator();
|
var enumerator = source.GetEnumerator();
|
||||||
while(enumerator.MoveNext())
|
while(enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
action?.Invoke(enumerator.Current);
|
action?.Invoke(enumerator.Current);
|
||||||
|
yield return enumerator.Current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user