c# Array.ForEach alternative that replaces elements -


Is there a standard function that I can use again on an array in ForEach, in each element Applying a function and changing it with the result in the array?

Something like the following, but later the dimension powers include the result of difference (2, I) on each element.

  Dimensions Powers = Numerical Category (0, dimension + 1) .oir (); Array.ForEach (Dimension Power, (i) => intoto (2, i));   

(I know that I can iterate with an array for loop - I'm just wondering if there is a more concise option).

Most types of orreter implementations (you actually need a "map" function) gives a new instance, for example an array, you can use LINQ:

  newArray = dimensionPowers.Select (i = & gt; IntPow (2, i)). ToArray ();   

Or better:

  dimension power = enumerable Category (0, dimension + 1). Choose (i => intoto (2, i)) .toArray ();   

However, if you want to modify the original array in-place, it is good to look old:

 for  (int i = 0; I   

It is enough for designers to use these different methods for NAT forces that when an array is modified, you will know.

If you want to implement your in-place revision, you can create an extension method:

  Fixed zero forEachModifyInplace & lt; T & gt; (This Array End Lt; T & A; Ere, Funk & lt; T, T & gt; Map_ Program) {for (int i = 0; i & lt; array.Length; i ++) {array [i] = map_action (Array [i]); }}    

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -