Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any extension method which get void returning lambda expression ?

For example

int[] Array = { 1, 23, 4, 5, 3, 3, 232, 32, };

Array.JustDo(x => Console.WriteLine(x));
like image 656
Freshblood Avatar asked Dec 01 '22 10:12

Freshblood


2 Answers

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));
like image 73
Brian R. Bondy Avatar answered Dec 09 '22 13:12

Brian R. Bondy


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);
        }
    }
}
like image 26
John Buchanan Avatar answered Dec 09 '22 15:12

John Buchanan