Array Arrays in C #

  • I need to know how to initialize an array of arrays in C # ..
  • I know that there is a multidimensional array, but I think that I do not need it in my case! I tried this code ... but could not know how to initialize the initialization list.

    double [] [] a = new double [2] []; // = {{1,2}, {3,4}};

thanks

PS: If you are interested in why I use it: I need a data structure that, when I call obj [0], returns an array .. I know this is strange ..

thanks

+7
arrays c # multidimensional-array initializer-list
source share
5 answers

Afaik, the easiest and most efficient way to press keys is to initialize an array with notches:

double[][] x = new []{new[]{1d, 2d}, new[]{3d, 4.3d}}; 

Edit:

This actually works too:

 double[][] x = {new[]{1d, 2d}, new[]{3d, 4.3d}}; 
+5
source share

This should work:

 double[][] a = new double[][] { new double[] {1.0d, 2.0d}, new double[] {3.0d, 4.0d} }; 
+4
source share

Since you have an array of arrays, you also need to create array objects inside it:

 double[][] a = new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 } }; 
+3
source share
 double[][] a = new double[][] { new double[] {1.0, 1.0}, new double[] {1.0, 1.0} }; 
+2
source share

I don’t know if I am right about this, but I used the so-called Structures in VB.net and I wonder how this concept is visible in C #. This refers to this question in this way:

 ' The declaration part Public Structure driveInfo Public type As String Public size As Long End Structure Public Structure systemInfo Public cPU As String Public memory As Long Public diskDrives() As driveInfo Public purchaseDate As Date End Structure ' this is the implementation part Dim allSystems(100) As systemInfo ReDim allSystems(1).diskDrives(3) allSystems(1).diskDrives(0).type = "Floppy" 

See how elegant it all is, and much better access than jagged arrays. How can all this be done in C # (maybe structures?)

0
source share

All Articles