C# Arrays


An array is a collection of variables that are of the same type. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. They are fixed in size, meaning that once you declare an array, you cannot change its size.

Array Basics

  1. Declaration: You declare an array by specifying the type of its elements followed by square brackets.
  2. Initialization: You can initialize an array at the time of declaration or later on.
  3. Accessing Elements: You access elements in an array using their index, which starts at 0.
  4. Multidimensional Arrays: C# supports multidimensional arrays, such as two-dimensional arrays (like matrices).

Example of Arrays in C#

Here's a detailed example that demonstrates various aspects of arrays in C#:

csharp
1using System; 2 3class Program 4{ 5 static void Main(string[] args) 6 { 7 // 1. Declaring and Initializing a One-Dimensional Array 8 int[] numbers = new int[5]; // Declare an array of 5 integers 9 10 // Initializing the array 11 for (int i = 0; i < numbers.Length; i++) 12 { 13 numbers[i] = i * 10; // Assign values: 0, 10, 20, 30, 40 14 } 15 16 // Displaying the elements of the array 17 Console.WriteLine("One-Dimensional Array:"); 18 foreach (var number in numbers) 19 { 20 Console.WriteLine(number); 21 } 22 23 // 2. Declaring and Initializing a Two-Dimensional Array 24 int[,] matrix = new int[3, 3]; // Declare a 3x3 matrix 25 26 // Initializing the two-dimensional array 27 for (int i = 0; i < matrix.GetLength(0); i++) 28 { 29 for (int j = 0; j < matrix.GetLength(1); j++) 30 { 31 matrix[i, j] = i + j; // Assign values based on the sum of indices 32 } 33 } 34 35 // Displaying the elements of the two-dimensional array 36 Console.WriteLine("\nTwo-Dimensional Array:"); 37 for (int i = 0; i < matrix.GetLength(0); i++) 38 { 39 for (int j = 0; j < matrix.GetLength(1); j++) 40 { 41 Console.Write(matrix[i, j] + "\t"); // Print with tab spacing 42 } 43 Console.WriteLine(); // New line for the next row 44 } 45 46 // 3. Declaring and Initializing an Array with Values 47 string[] fruits = { "Apple", "Banana", "Cherry", "Date" }; // Implicitly declare and initialize 48 49 // Displaying the elements of the string array 50 Console.WriteLine("\nFruits Array:"); 51 foreach (var fruit in fruits) 52 { 53 Console.WriteLine(fruit); 54 } 55 56 // 4. Using Array Methods 57 Array.Sort(fruits); // Sort the array of fruits 58 Console.WriteLine("\nSorted Fruits Array:"); 59 foreach (var fruit in fruits) 60 { 61 Console.WriteLine(fruit); 62 } 63 64 // 5. Finding the Length of an Array 65 Console.WriteLine($"\nThe length of the numbers array is: {numbers.Length}"); 66 } 67}

Explanation of the Example

  1. One-Dimensional Array:

    • We declare an integer array numbers with a size of 5.
    • We initialize the array using a for loop, assigning values that are multiples of 10.
    • We display the contents of the array using a foreach loop.
  2. Two-Dimensional Array:

    • We declare a 3x3 integer matrix matrix.
    • We initialize the matrix using nested for loops, where each element is assigned the sum of its indices.
    • We display the matrix in a formatted manner using nested loops.
  3. Implicitly Declared Array:

    • We declare and initialize a string array fruits with a list of fruit names.
    • We display the contents of this array using a foreach loop.
  4. Using Array Methods:

    • We use the Array.Sort method to sort the fruits array alphabetically.
    • We display the sorted array.
  5. Finding the Length of an Array:

    • We use the Length property of the numbers array to find and display its length.

C# Arrays

1. Multi-dimensional Arrays

Description: C# supports multidimensional arrays, which are arrays with more than one dimension. The simplest form is the two-dimensional array, which can be thought of as a grid or a table.

Example:

csharp
1using System; 2 3class MultiDimensionalArrayExample 4{ 5 static void Main(string[] args) 6 { 7 // Declaring and initializing a 2D array (3 rows and 3 columns) 8 int[,] matrix = new int[3, 3] 9 { 10 { 1, 2, 3 }, 11 { 4, 5, 6 }, 12 { 7, 8, 9 } 13 }; 14 15 // Displaying the 2D array 16 Console.WriteLine("2D Array (Matrix):"); 17 for (int i = 0; i < matrix.GetLength(0); i++) 18 { 19 for (int j = 0; j < matrix.GetLength(1); j++) 20 { 21 Console.Write(matrix[i, j] + "\t"); 22 } 23 Console.WriteLine(); 24 } 25 } 26}

2. Jagged Arrays

Description: Jagged arrays are arrays of arrays. Unlike multidimensional arrays, jagged arrays can have different lengths for each row, making them more flexible.

Example:

csharp
1using System; 2 3class JaggedArrayExample 4{ 5 static void Main(string[] args) 6 { 7 // Declaring and initializing a jagged array 8 int[][] jaggedArray = new int[3][]; 9 jaggedArray[0] = new int[] { 1, 2, 3 }; 10 jaggedArray[1] = new int[] { 4, 5 }; 11 jaggedArray[2] = new int[] { 6, 7, 8, 9 }; 12 13 // Displaying the jagged array 14 Console.WriteLine("Jagged Array:"); 15 for (int i = 0; i < jaggedArray.Length; i++) 16 { 17 for (int j = 0; j < jaggedArray[i].Length; j++) 18 { 19 Console.Write(jaggedArray[i][j] + "\t"); 20 } 21 Console.WriteLine(); 22 } 23 } 24}

3. Passing Arrays to Functions

Description: You can pass an array to a function by specifying the array's name without an index. This allows the function to access and manipulate the array elements.

Example:

csharp
1using System; 2 3class PassingArraysExample 4{ 5 static void Main(string[] args) 6 { 7 int[] numbers = { 1, 2, 3, 4, 5 }; 8 Console.WriteLine("Sum of array elements: " + SumArray(numbers)); 9 } 10 11 static int SumArray(int[] arr) 12 { 13 int sum = 0; 14 foreach (var number in arr) 15 { 16 sum += number; 17 } 18 return sum; 19 } 20}

4. Param Arrays

Description: The params keyword allows you to pass a variable number of arguments to a method. This is useful when you do not know how many arguments will be passed at compile time.

Example:

csharp
1using System; 2 3class ParamArrayExample 4{ 5 static void Main(string[] args) 6 { 7 Console.WriteLine("Sum of numbers: " + Sum(1, 2, 3, 4, 5)); 8 Console.WriteLine("Sum of numbers: " + Sum(10, 20)); 9 } 10 11 static int Sum(params int[] numbers) 12 { 13 int sum = 0; 14 foreach (var number in numbers) 15 { 16 sum += number; 17 } 18 return sum; 19 } 20}

5. The Array Class

Description: The Array class, defined in the System namespace, is the base class for all arrays in C#. It provides various properties and methods for working with arrays, such as sorting, searching, and manipulating array elements.

Example:

csharp
1using System; 2 3class ArrayClassExample 4{ 5 static void Main(string[] args) 6 { 7 int[] numbers = { 5, 2, 8, 3, 1 }; 8 9 // Using the Array class to sort the array 10 Array.Sort(numbers); 11 Console.WriteLine("Sorted Array:"); 12 foreach (var number in numbers) 13 { 14 Console.Write(number + " "); 15 } 16 17 // Using the Array class to find the index of an element 18 int index = Array.IndexOf(numbers, 3); 19 Console.WriteLine($"\nIndex of 3 in the array: {index}"); 20 } 21}

An array is a collection of variables that are of the same type. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. They are fixed in size, meaning that once you declare an array, you cannot change its size.

Array Basics

  1. Declaration: You declare an array by specifying the type of its elements followed by square brackets.
  2. Initialization: You can initialize an array at the time of declaration or later on.
  3. Accessing Elements: You access elements in an array using their index, which starts at 0.
  4. Multidimensional Arrays: C# supports multidimensional arrays, such as two-dimensional arrays (like matrices).

Example of Arrays in C#

Here's a detailed example that demonstrates various aspects of arrays in C#:

csharp
1using System; 2 3class Program 4{ 5 static void Main(string[] args) 6 { 7 // 1. Declaring and Initializing a One-Dimensional Array 8 int[] numbers = new int[5]; // Declare an array of 5 integers 9 10 // Initializing the array 11 for (int i = 0; i < numbers.Length; i++) 12 { 13 numbers[i] = i * 10; // Assign values: 0, 10, 20, 30, 40 14 } 15 16 // Displaying the elements of the array 17 Console.WriteLine("One-Dimensional Array:"); 18 foreach (var number in numbers) 19 { 20 Console.WriteLine(number); 21 } 22 23 // 2. Declaring and Initializing a Two-Dimensional Array 24 int[,] matrix = new int[3, 3]; // Declare a 3x3 matrix 25 26 // Initializing the two-dimensional array 27 for (int i = 0; i < matrix.GetLength(0); i++) 28 { 29 for (int j = 0; j < matrix.GetLength(1); j++) 30 { 31 matrix[i, j] = i + j; // Assign values based on the sum of indices 32 } 33 } 34 35 // Displaying the elements of the two-dimensional array 36 Console.WriteLine("\nTwo-Dimensional Array:"); 37 for (int i = 0; i < matrix.GetLength(0); i++) 38 { 39 for (int j = 0; j < matrix.GetLength(1); j++) 40 { 41 Console.Write(matrix[i, j] + "\t"); // Print with tab spacing 42 } 43 Console.WriteLine(); // New line for the next row 44 } 45 46 // 3. Declaring and Initializing an Array with Values 47 string[] fruits = { "Apple", "Banana", "Cherry", "Date" }; // Implicitly declare and initialize 48 49 // Displaying the elements of the string array 50 Console.WriteLine("\nFruits Array:"); 51 foreach (var fruit in fruits) 52 { 53 Console.WriteLine(fruit); 54 } 55 56 // 4. Using Array Methods 57 Array.Sort(fruits); // Sort the array of fruits 58 Console.WriteLine("\nSorted Fruits Array:"); 59 foreach (var fruit in fruits) 60 { 61 Console.WriteLine(fruit); 62 } 63 64 // 5. Finding the Length of an Array 65 Console.WriteLine($"\nThe length of the numbers array is: {numbers.Length}"); 66 } 67}

Explanation of the Example

  1. One-Dimensional Array:

    • We declare an integer array numbers with a size of 5.
    • We initialize the array using a for loop, assigning values that are multiples of 10.
    • We display the contents of the array using a foreach loop.
  2. Two-Dimensional Array:

    • We declare a 3x3 integer matrix matrix.
    • We initialize the matrix using nested for loops, where each element is assigned the sum of its indices.
    • We display the matrix in a formatted manner using nested loops.
  3. Implicitly Declared Array:

    • We declare and initialize a string array fruits with a list of fruit names.
    • We display the contents of this array using a foreach loop.
  4. Using Array Methods:

    • We use the Array.Sort method to sort the fruits array alphabetically.
    • We display the sorted array.
  5. Finding the Length of an Array:

    • We use the Length property of the numbers array to find and display its length.

C# Arrays

1. Multi-dimensional Arrays

Description: C# supports multidimensional arrays, which are arrays with more than one dimension. The simplest form is the two-dimensional array, which can be thought of as a grid or a table.

Example:

csharp
1using System; 2 3class MultiDimensionalArrayExample 4{ 5 static void Main(string[] args) 6 { 7 // Declaring and initializing a 2D array (3 rows and 3 columns) 8 int[,] matrix = new int[3, 3] 9 { 10 { 1, 2, 3 }, 11 { 4, 5, 6 }, 12 { 7, 8, 9 } 13 }; 14 15 // Displaying the 2D array 16 Console.WriteLine("2D Array (Matrix):"); 17 for (int i = 0; i < matrix.GetLength(0); i++) 18 { 19 for (int j = 0; j < matrix.GetLength(1); j++) 20 { 21 Console.Write(matrix[i, j] + "\t"); 22 } 23 Console.WriteLine(); 24 } 25 } 26}

2. Jagged Arrays

Description: Jagged arrays are arrays of arrays. Unlike multidimensional arrays, jagged arrays can have different lengths for each row, making them more flexible.

Example:

csharp
1using System; 2 3class JaggedArrayExample 4{ 5 static void Main(string[] args) 6 { 7 // Declaring and initializing a jagged array 8 int[][] jaggedArray = new int[3][]; 9 jaggedArray[0] = new int[] { 1, 2, 3 }; 10 jaggedArray[1] = new int[] { 4, 5 }; 11 jaggedArray[2] = new int[] { 6, 7, 8, 9 }; 12 13 // Displaying the jagged array 14 Console.WriteLine("Jagged Array:"); 15 for (int i = 0; i < jaggedArray.Length; i++) 16 { 17 for (int j = 0; j < jaggedArray[i].Length; j++) 18 { 19 Console.Write(jaggedArray[i][j] + "\t"); 20 } 21 Console.WriteLine(); 22 } 23 } 24}

3. Passing Arrays to Functions

Description: You can pass an array to a function by specifying the array's name without an index. This allows the function to access and manipulate the array elements.

Example:

csharp
1using System; 2 3class PassingArraysExample 4{ 5 static void Main(string[] args) 6 { 7 int[] numbers = { 1, 2, 3, 4, 5 }; 8 Console.WriteLine("Sum of array elements: " + SumArray(numbers)); 9 } 10 11 static int SumArray(int[] arr) 12 { 13 int sum = 0; 14 foreach (var number in arr) 15 { 16 sum += number; 17 } 18 return sum; 19 } 20}

4. Param Arrays

Description: The params keyword allows you to pass a variable number of arguments to a method. This is useful when you do not know how many arguments will be passed at compile time.

Example:

csharp
1using System; 2 3class ParamArrayExample 4{ 5 static void Main(string[] args) 6 { 7 Console.WriteLine("Sum of numbers: " + Sum(1, 2, 3, 4, 5)); 8 Console.WriteLine("Sum of numbers: " + Sum(10, 20)); 9 } 10 11 static int Sum(params int[] numbers) 12 { 13 int sum = 0; 14 foreach (var number in numbers) 15 { 16 sum += number; 17 } 18 return sum; 19 } 20}

5. The Array Class

Description: The Array class, defined in the System namespace, is the base class for all arrays in C#. It provides various properties and methods for working with arrays, such as sorting, searching, and manipulating array elements.

Example:

csharp
1using System; 2 3class ArrayClassExample 4{ 5 static void Main(string[] args) 6 { 7 int[] numbers = { 5, 2, 8, 3, 1 }; 8 9 // Using the Array class to sort the array 10 Array.Sort(numbers); 11 Console.WriteLine("Sorted Array:"); 12 foreach (var number in numbers) 13 { 14 Console.Write(number + " "); 15 } 16 17 // Using the Array class to find the index of an element 18 int index = Array.IndexOf(numbers, 3); 19 Console.WriteLine($"\nIndex of 3 in the array: {index}"); 20 } 21}

Talk to us?

Post your blog