c# 4.0 - C#: Setting all values in an array with arbitrary dimensions -


I am looking for a way to set one value to every value in a multi-dimensional array. The problem is that in the compilation time the number of dimensions is unknown - it can be one-dimensional, it can be 4-dimensional because foreach does not let you set the values, the only way Can I achieve this goal? Thank you very much.

Although this problem looks simple on the surface, it is actually more complex than looks like. However, by detecting every situation in multi-dimensional (or even jagged) array, Cartesian Product Operations on the set of array of arrays - we can simplify the solution. ..and finally write a more elegant solution.

We are going to take advantage of the implementation to lift heavy load. You can read more about how you work on your blog if you like

However, this implementation is specific to the cells of a multi-dimensional array - it should be relatively easy to see how it should be extended to go on an extended array.

Public static class EnumerableExt {// Eric Lipert's Cartesian Product Operator ... Public Fixed IEnumerable & lt; IEnumerable & lt; T & gt; & Gt; Cartesian product & lt; T & gt; (This IEnumerable & lt; IEnumerable & lt; T & gt; & gt; Sequence) {IEnumerable & lt; IEnumerable & lt; T & gt; & Gt; Empty Products = New [] {Enumerable.Alty & lt; T & gt; ()}; Return Sequences Select accseq.Concat (new [] {item}) from the item in the aggregate (empty product, (accumulator, sequence) = & gt; in the cache;}} class MDFill {public static void Main () {// An arbitrary multidimensional Create array Array mdArray = new int [2,3,4,5]; // MD-Array var dimension bound = enumerable.Remember within the range (0, mdArray.Rank) to represent all possible // pointer positions of each dimension Select a view of the visuals (x => enfable.ange (mdArray. GetLowerBound (x), mdArray.GetUpperBound (x) - mdArray.GetLowerBound (x) +1)) // inde Use the Cartesian product to see every sequence of ss / // MD in the array and set each position in a specific value ... int someValue = 100; foreign currency (set in different index dimensions Bowles. ()) {MdArray.SetValue (someValue, indexSet.ToArray ());}}}

Now this code is comparable to the factor in a reusable method that either Can be used for jagged or multi-functional arrays ... or Oi data structure can be seen as the rectangular array.


Comments