Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would recommend you just use a normal <code>foreach</code> loop to transform the data. You're using a method that exists only on the <code>List&lt;T&gt;</code> implementation, but not on arrays.</p> <p>Using a method for <code>foreach</code> really gains you nothing, unless for some reason you were wanting to do data mutation in a method chain. In that case, you may as well write your own extension method for <code>IEnumerable&lt;T&gt;</code>. I would recommend against that, though.</p> <p>Having a separate <code>foreach</code> loop makes it clear to the reader that data mutation is occurring. It also removes the overhead of calling a delegate for each iteration of the loop. It will also work regardless of the collection type as long as it is an <code>IEnumerable</code> (not entirely true, you can write your own enumerators and enumerables, but that's a different question).</p> <p>If you're looking to just do data transformations (i.e. projections and the like) then use LINQ.</p> <p>Also keep in mind that with the array, you're getting a copy of the <code>byte</code> not a reference. You'll be modifying just that <code>byte</code> not the original. Here's an example with the output:</p> <pre><code>int[] numbers = new int[] { 1, 2, 3, 4, 5 }; Array.ForEach(numbers, number =&gt; number += 1); foreach(int number in numbers) { Console.WriteLine(number); } </code></pre> <p>Which yields the output:</p> <blockquote> <p>1 2 3 4 5</p> </blockquote> <p>As you can see, the number += 1 in the lambda had no effect. In fact, if you tried this in a normal <code>foreach</code> loop, you would get a compiler error. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload