Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 24 / 68

Two-Dimensional Arrays (Tabular Data)

In this lesson you will learn how to use two-dimensional arrays in C#, so you can work with data laid out as a table. This is for anyone searching for "C# 2D array how to use".

Writing int[,] grid gives you a grid with rows and columns. This is called a two-dimensional array, and you access it as grid[row, column].

The sample code declares two rows of three columns with int[,] grid = { {1, 2, 3}, {4, 5, 6} } and reads a value with a comma-separated row and column, as in grid[1, 2]. Notice how nested for loops print the whole thing.

A common stumbling block for beginners is the order of the indices. grid[1, 2] means "row 1, column 2", and mixing up which is which gives you the wrong value.

In real-world development, you can also express three or more dimensions, or jagged arrays (arrays of arrays), and pick whichever fits the job.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)