C
C#16mo ago
Ronnie

✅ help with 2D arrays

using System;
using System.Collections.Generic;


namespace CSharp
{
class Program
{
static void Main(string[] args)
{

int[,] arr = { {1, 2, 3}, {4, 5, 6} };

foreach (int i in arr){
Console.WriteLine(arr[i]);
}
}
}
}
using System;
using System.Collections.Generic;


namespace CSharp
{
class Program
{
static void Main(string[] args)
{

int[,] arr = { {1, 2, 3}, {4, 5, 6} };

foreach (int i in arr){
Console.WriteLine(arr[i]);
}
}
}
}
How would i loop thorugh a 2d array and print their values instead of their index? this is hat i have so far
8 Replies
ero
ero16mo ago
this doesn't even compile
Ronnie
Ronnie16mo ago
how do i fix this
ero
ero16mo ago
you need 2 loops, one nested inside the other the outer loops over dimension 0, the inner loops over dimension 1
Ronnie
Ronnie16mo ago
oh
ero
ero16mo ago
you get their lengths via arr.GetLength(0), arr.GetLength(1)
Ronnie
Ronnie16mo ago
oh so no foreach loop?
ero
ero16mo ago
no
Ronnie
Ronnie16mo ago
ok thanks