Betamoo
Betamoo

Reputation: 15960

Array of Arrays in C#

Thank you

PS: If you wonder why I use it: I need data structure that when I call obj[0] it returns an array.. I know it is strange..

Thanks

Upvotes: 7

Views: 836

Answers (5)

netfed
netfed

Reputation: 601

I don't know if I'm right about this, but I have been using socalled Structures in VB.net, and wondering how this concept is seen in C#. It is relevant 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 all this is, and far better to access than jagged arrays. How can all this be done in C# (structs maybe?)

Upvotes: 0

Pop Catalin
Pop Catalin

Reputation: 62970

Afaik, the most simple and keystroke effective way is this to initialize a jagged array is:

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

Edit:

Actually this works too:

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

Upvotes: 5

Taylor Leese
Taylor Leese

Reputation: 52390

double[][] a = new double[][] {
     new double[] {1.0, 1.0}, 
     new double[] {1.0, 1.0}
};

Upvotes: 2

Guffa
Guffa

Reputation: 700730

As you have an array of arrays, you have to create the array objects inside it also:

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

Upvotes: 3

Robert Harvey
Robert Harvey

Reputation: 180898

This should work:

double[][] a = new double[][] 
{ 
    new double[] {1.0d, 2.0d},
    new double[] {3.0d, 4.0d}
};

Upvotes: 4

Related Questions