S
Shapper
Hello,
I created a ForEach extension as follows:
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action) {
foreach (T item in source) {
action(item);
yield return item;
}
} // ForEach
However I need to use ToList() at the end to apply the actions.
So I created 2 other extensions and change the ForEach code:
IEnumerable<T> Apply<T>(this IEnumerable<T> source, Action<T> action) {
foreach (var item in source) {
action(item);
yield return item;
}
}
And then have another one as follows:
IEnumerable<T> Lock<T>(this IEnumerable<T> source) {
foreach (var item in source) {
; // Force to apply changes
}
return source;
}
This would be used as follows:
collection.Apply(....).Apply(....).Lock();
Does this make sense?
And the new ForEach extension would be:
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (T item in source)
action(item);
}
However, using Apply and Lock I think this does not make sense to have.
What do you think?
I created a ForEach extension as follows:
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action) {
foreach (T item in source) {
action(item);
yield return item;
}
} // ForEach
However I need to use ToList() at the end to apply the actions.
So I created 2 other extensions and change the ForEach code:
IEnumerable<T> Apply<T>(this IEnumerable<T> source, Action<T> action) {
foreach (var item in source) {
action(item);
yield return item;
}
}
And then have another one as follows:
IEnumerable<T> Lock<T>(this IEnumerable<T> source) {
foreach (var item in source) {
; // Force to apply changes
}
return source;
}
This would be used as follows:
collection.Apply(....).Apply(....).Lock();
Does this make sense?
And the new ForEach extension would be:
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (T item in source)
action(item);
}
However, using Apply and Lock I think this does not make sense to have.
What do you think?