Is there any extension method which get void returning lambda expression ?
For example
int[] Array = { 1, 23, 4, 5, 3, 3, 232, 32开发者_Go百科, };
Array.JustDo(x => Console.WriteLine(x));
I think you're looking for Array.ForEach which doesn't require you to convert to a List<> first.
int[] a = { 1, 23, 4, 5, 3, 3, 232, 32, };
Array.ForEach(a, x => Console.WriteLine(x));
You can use the Array.ForEach method
int[] array = { 1, 2, 3, 4, 5};
Array.ForEach(array, x => Console.WriteLine(x));
or make your own extension method
void Main()
{
int[] array = { 1, 2, 3, 4, 5};
array.JustDo(x => Console.WriteLine(x));
}
public static class MyExtension
{
public static void JustDo<T>(this IEnumerable<T> ext, Action<T> a)
{
foreach(T item in ext)
{
a(item);
}
}
}
As others have said, you can use Array.ForEach
. However, you might like to read Eric Lippert's thoughts on this.
If you still want it after reading that, there is the Do
method in the System.Interactive
assembly which is part of Reactive Extensions, as part of EnumerableEx
. You'd use it like this:
int[] array = { 1, 23, 4, 5, 3, 3, 232, 32, };
array.Do(x => Console.WriteLine(x));
(I've changed the name of the variable to avoid confusion. It's generally better not to name variables with the same name as a type.)
There's plenty of great stuff to look at in Reactive Extensions...
精彩评论