foreach - replace a simple forloop with linq -


I want to know how to replace a simple foreach loop with linq i answer about 2 or more loops I'm not looking ... it's for a special foreach loop ..

  list & lt; String & gt; Strlist = new list & lt; String & gt; (); Strlist.Add ("hello"); Strlist.Add ("World"); // main "to be linq here" ... foreach (strlist string in strlist) {console. Writeline (str); }  

Now how do I write this simple loop in 1 line?

Thank you

This type of loop is not to be written as an expression.

Use only query expressions if the code does not have any side effects and creates a value.

In this case, you are looping a statement repeating, which has a side effect on the console and does not return the value. So a football loop is clear and specially designed for this purpose.

On the other hand, an action (which can have a side effect) can be considered as a pure value before executing it . So here's a list of numbers:

  list & lt; Int & gt; Numbers = Exclusive Category (1, 10) .Oolist ();  

From that we make a list of functions:

  list & lt; Action & gt; Verb = number. Select (n = & gt; console.light line (n)). ToList ();  

Although we are working with tasks that deal with such side effects, but we are not actually running them at all, so the content of that list is more anti-competitive Are not there. Then at the end when we need a list, we can use a forloop to execute it:

  foreach (one in task);  

And this is a simple pattern, it can be argued that on RunAll extensions IEnumerable & lt; Action & gt; Will not be bad. Actually, this concept is created in the .NET framework: A multicast representative is one thing you can call which executes a group of delegates in a list. In the most common use cases (incidents), those representatives have side effects.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -