0

you know that in java you can use the rows of an array just by calling the index of the row, which is the equivalent in c#?

int[][] matrix = {{0,1},{2,3}};

If I want to use row 0, I only reference it like this:

int[] row1 = matrix[0];

row values will be:

{0,1}

Now how do I do this in c #? with the:

  int[,] matrix = {{0,1},{2,3}};

If I refer to the array with a single value I receive: enter image description here

int[] row = matrix[j];

I thank you

cacasswein
  • 105
  • 1
  • 6

1 Answers1

3

The java and the c# are not the same. Your c# uses a multidimensional array and your java uses a jagged

Change your c# to a jagged and init it:

int[][] matrix = {new[]{0,1},new[]{2,3}};

The slight syntax different here versus java is that you have to new[] the inner arrays but you can omit the type because the compiler is willing to infer it from the type it sees the elements as inside the curly brackets. Omitting the new[] entirely gives a helpful compiler error outlining that array initializers can only be used in assignments (and this inner array is not an assignment in that sense) and suggests the use of "new" again :)

C# documentation on jagged arrays has lots of useful tips: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays

Caius Jard
  • 72,509
  • 5
  • 49
  • 80