
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to Loop over a two dimensional array
Declare a two dimensional array −
string[,] array = new string[3, 3];
Set elements in the array −
array[0, 0] = "One"; array[0, 1] = "Two"; array[0, 2] = "Three"; array[1, 0] = "Four"; array[1, 1] = "Five"; array[1, 2] = "Six"; array[2, 0] = "Seven"; array[2, 1] = "Eight"; array[2, 2] = "Nine";
Now, get the upper bound to get the dimensions to loop over the array −
int uBound0 = array.GetUpperBound(0); int uBound1 = array.GetUpperBound(1);
Iterate through a nested loop, until the above two values as shown in the code below −
Example
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { string[,] array = new string[3, 3]; array[0, 0] = "One"; array[0, 1] = "Two"; array[0, 2] = "Three"; array[1, 0] = "Four"; array[1, 1] = "Five"; array[1, 2] = "Six"; array[2, 0] = "Seven"; array[2, 1] = "Eight"; array[2, 2] = "Nine"; // getting upper bound int uBound0 = array.GetUpperBound(0); int uBound1 = array.GetUpperBound(1); for (int i = 0; i <= uBound0; i++) { for (int j = 0; j <= uBound1; j++) { string res = array[i, j]; Console.WriteLine(res); } } Console.ReadLine(); } }
Output
One Two Three Four Five Six Seven Eight Nine
- Related Articles
- C# program to iterate over a string array with for loop
- Java Program to create DefaultTableModel from two dimensional array
- Golang Program to read and print two-dimensional array
- Split one-dimensional array into two-dimensional array JavaScript
- Passing two dimensional array to a C++ function
- How to create a two dimensional array in JavaScript?
- How to declare a two-dimensional array in C#
- How to create a two-dimensional array in TypeScript?
- Transpose of a two-dimensional array - JavaScript
- C Program on two-dimensional array initialized at run time
- Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
- What is a two-dimensional array in C language?
- Get the Inner product of a One-Dimensional and a Two-Dimensional array in Python
- Java Program to convert array to String for one dimensional and multi-dimensional arrays
- C++ Program to Iterate Over an Array

Advertisements