C
C#2y 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
ero2y ago
this doesn't even compile
Ronnie
RonnieOP2y ago
how do i fix this
ero
ero2y ago
you need 2 loops, one nested inside the other the outer loops over dimension 0, the inner loops over dimension 1
Ronnie
RonnieOP2y ago
oh
ero
ero2y ago
you get their lengths via arr.GetLength(0), arr.GetLength(1)
Ronnie
RonnieOP2y ago
oh so no foreach loop?
ero
ero2y ago
no
Ronnie
RonnieOP2y ago
ok thanks
Want results from more Discord servers?
Add your server