Is there any extension method that gets an empty lambda return expression?

for instance

int[] Array = { 1, 23, 4, 5, 3, 3, 232, 32, }; Array.JustDo(x => Console.WriteLine(x)); 
+4
source share
3 answers

I think you are looking for Array.ForEach , which does not 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)); 
+12
source

You can use the Array.ForEach method

 int[] array = { 1, 2, 3, 4, 5}; Array.ForEach(array, x => Console.WriteLine(x)); 

or create 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); } } } 
+6
source

As others have said, you can use Array.ForEach . However, you can read Eric Lippert's thoughts on this .

If you still want to read it after that, there is a Do method in the System.Interactive assembly that is part of Reactive Extensions , as part of EnumerableEx . You would use it as follows:

 int[] array = { 1, 23, 4, 5, 3, 3, 232, 32, }; array.Do(x => Console.WriteLine(x)); 

(I changed the variable name to avoid confusion. It is usually best not to specify variables with the same name as the type.)

Reactive Extensions has a lot of great things ...

+2
source

Source: https://habr.com/ru/post/1312702/


All Articles