Thursday 15 January 2015

C# Initializing a Multi-dimensional array with multiple 1D arrays -


I am trying to start two-dimensional array using two existing 1D arrays. Obviously, if we know the values ​​of these arrays, then we can start this way:

  float [,] my2DArray = new float {{1,2}, {3 , 4}};  

However, if I try to start the array with such a variable:

  float [] a = {1,2} ; Float [] b = {3,4}; Float [,] my2DArray = new float {a, b};  

Then I get an error "A Nested Array Starter is expected." I have thought that maybe this is something to do with the compiler, since the array it will not be assigned to the runtime

So, is there any way by which I can assign such array Can i work for I'm targeting NAT 4.0

You have to start the array yourself using performance improvements and smaller codes than looping , But can not directly inline initialization:

  float [] a = {1,2}; Float [] b = {3,4}; Float [,] my2DArray = new float [a.Length, 2]; Int len ​​= a.Length * sizeof (float); Buffer BlockCopy (A, 0, My 2DERR, 0, Lane); Buffer BlockCopy (B, 0, My 2 Dina, Lane, Lane);  

Note that you must guarantee that the source arrays are the same lengths for doing this.


No comments:

Post a Comment