Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

loop through multidimensional array c#

for (int x = 0; x < YourArray.GetLength(0); x++)
{
    for (int y = 0; y < YourArray.GetLength(1); y++)
    {
        Console.WriteLine(YourArray[x, y]);
    }
}
Comment

iterate 2d array in c#

If you want to iterate over every item in the array as if it were a flattened array, you can just do:

foreach (int i in array) {
    Console.Write(i);
}
which would print 123456

If you want to be able to know the x and y indexes as well, you'll need to do:

for (int x = 0; x < array.GetLength(0); x += 1) {
    for (int y = 0; y < array.GetLength(1); y += 1) {
        Console.Write(array[x, y]);
    }
}
Alternatively you could use a jagged array instead (an array of arrays):

int[][] array = new int[2][] { new int[3] {1, 2, 3}, new int[3] {4, 5, 6} };
foreach (int[] subArray in array) {
    foreach (int i in subArray) {
        Console.Write(i);
    }
}
or

int[][] array = new int[2][] { new int[3] {1, 2, 3}, new int[3] {4, 5, 6} };
for (int j = 0; j < array.Length; j += 1) {
    for (int k = 0; k < array[j].Length; k += 1) {
        Console.Write(array[j][k]);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# main method 
Csharp :: get random number c# 
Csharp :: unity destroy object when out of screen 
Csharp :: wpf choose file dialog 
Csharp :: swagger authentication bearer .net core 
Csharp :: radians to degree c# 
Csharp :: Unity rotate player to mouse point slowly 
Csharp :: how to instantiate as child unity 
Csharp :: c# json to dictionary 
Csharp :: how to draw text in monogame 
Csharp :: set textbox colour to transparent c# 
Csharp :: system.text.json DeserializeAsync when to use 
Csharp :: unity ui change sprite 
Csharp :: get type of exception c# 
Csharp :: xml node update attribute value c# 
Csharp :: unity get direction from one point to another 
Csharp :: addding two numebrs with c# 
Csharp :: vs code explorer font size 
Csharp :: wpf image clip with rounded corners 
Csharp :: c# check valid datetime 
Csharp :: C# inline question mark on object 
Csharp :: asp.net throw unauthorized exception 
Csharp :: start new form c# 
Csharp :: loan calculator using windows forms in c# code 
Csharp :: fair division 
Csharp :: c# reverse a string 
Csharp :: get length of enum values 
Csharp :: c# read char 
Csharp :: readonly vs const c# 
Csharp :: set object to random color unity 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =