Arrays in C#
Arrays in C#
using System;
class Program
# Program Structure
The program consists of a single class Program with a Main method, which is the entry point of the
program.
- numbers: an integer array (int[]) that stores the elements { 10, 20, 30, 40, 50 }.
- args: a string array (string[]) that represents the command-line arguments (not used in this program).
# Program Flow
1. Array Definition: The program defines an integer array numbers with five elements: { 10, 20, 30, 40,
50 }.
2. Display Array Elements: The program uses a for loop to iterate over the elements of the numbers
array.
3. Loop Counter: The loop counter i is initialized to 0 and incremented by 1 in each iteration.
4. Array Indexing: The program uses the loop counter i to access each element of the numbers array
using the syntax numbers[i].
5. Display Element: In each iteration, the program displays the current element using the syntax
Console.WriteLine($"Element {i + 1}: {numbers[i]}").
6. Loop Termination: The loop terminates when the loop counter i reaches the length of the numbers
array (numbers.Length).
Multidimensional Array
using System;
class Program
// Single-dimensional array
int[] singleArray = { 1, 2, 3, 4, 5 };
Console.WriteLine("Single-dimensional array:");
Console.WriteLine();
int[,] multiArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Console.WriteLine();
# Program Structure
The program consists of a single class Program with a Main method, which is the entry point of the
program.
# Program Flow
Single-Dimensional Array
1. Array Definition: The program defines a single-dimensional integer array singleArray with five
elements: { 1, 2, 3, 4, 5 }.
2. Display Array Elements: The program uses a foreach loop to iterate over the elements of the
singleArray.
3. Display Element: In each iteration, the program displays the current element using
Console.Write(num + " ").
Multidimensional Array (2D)
1. Array Definition: The program defines a multidimensional integer array multiArray with three rows
and three columns: { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }.
2. Get Length of Array: The program uses the GetLength method to get the number of rows and columns
in the multiArray.
3. Nested Loops: The program uses two nested for loops to iterate over the elements of the multiArray.
4. Display Element: In each iteration, the program displays the current element using
Console.Write(multiArray[i, j] + " ").
# Output
Single-dimensional array:
12345
123
456
789