C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we'll demonstrate how to work with arrays in C#. An array is a fixed-size, indexed collection of elements of the same type. Arrays are helpful when you need to store and manipulate a collection of items in a program.
Create a new C# Console Application project in Visual Studio.
Declare and initialize an array of integers with five elements:
int[] numbers = new int[5] { 10, 20, 30, 40, 50 };
Alternatively, you can use a shorter syntax to declare and initialize an array:
int[] numbers = { 10, 20, 30, 40, 50 };
Array elements are accessed using their index, which starts from 0. To access the first element in the numbers
array, use the following syntax:
int firstNumber = numbers[0];
You can use a for
loop to iterate through the elements of an array:
for (int i = 0; i < numbers.Length; i++) { Console.WriteLine($"Element {i}: {numbers[i]}"); }
Alternatively, you can use a foreach
loop to iterate through the elements of an array:
foreach (int number in numbers) { Console.WriteLine($"Element: {number}"); }
C# supports multidimensional arrays. To declare and initialize a two-dimensional array, use the following syntax:
int[,] matrix = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
To access an element in a multidimensional array, use row and column indices:
int element = matrix[1, 0]; // This will get the value 3
You can use nested for
loops to iterate through a two-dimensional array:
for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { Console.Write($"{matrix[i, j]} "); } Console.WriteLine(); }
Arrays in C# have several useful methods, such as Sort()
, Reverse()
, and IndexOf()
. For example:
int[] numbers = { 10, 20, 30, 40, 50 }; Array.Sort(numbers); // Sorts the array in ascending order Array.Reverse(numbers); // Reverses the order of the elements in the array int index = Array.IndexOf(numbers, 30); // Returns the index of the first occurrence of the value 30 in the array
In this tutorial, we've shown you how to work with arrays in C#. Arrays are a fundamental data structure in C# and provide an efficient way to store and manipulate a fixed-size collection of items of the same type.
C# array example:
// Declare and initialize an array int[] numbers = { 1, 2, 3, 4, 5 }; // Access and print elements of the array foreach (int number in numbers) { Console.WriteLine(number); }
How to declare and initialize arrays in C#:
// Declare and initialize an array using explicit size int[] numbers1 = new int[5]; // Declare and initialize an array with initial values int[] numbers2 = { 1, 2, 3, 4, 5 };
Accessing elements in a C# array:
int[] numbers = { 1, 2, 3, 4, 5 }; // Access and print specific elements Console.WriteLine(numbers[0]); // Output: 1 Console.WriteLine(numbers[2]); // Output: 3
Iterating through arrays in C#:
foreach
and traditional for
loops.int[] numbers = { 1, 2, 3, 4, 5 }; // Using foreach loop foreach (int number in numbers) { Console.WriteLine(number); } // Using for loop for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); }
Array length property in C#:
Length
property to determine the size of an array in C#.int[] numbers = { 1, 2, 3, 4, 5 }; // Print the length of the array Console.WriteLine($"Array length: {numbers.Length}");
Sorting arrays in C#:
Array.Sort
method.int[] numbers = { 5, 3, 1, 4, 2 }; // Sort the array in ascending order Array.Sort(numbers); // Print the sorted array foreach (int number in numbers) { Console.WriteLine(number); }
Searching for an element in an array C#:
Array.IndexOf
.int[] numbers = { 1, 2, 3, 4, 5 }; int target = 3; // Find the index of the target element int index = Array.IndexOf(numbers, target); // Print the index or a message if not found Console.WriteLine(index != -1 ? $"Element found at index {index}" : "Element not found");
Modifying elements in a C# array:
int[] numbers = { 1, 2, 3, 4, 5 }; // Modify the third element numbers[2] = 10; // Print the modified array foreach (int number in numbers) { Console.WriteLine(number); }
Multidimensional arrays in C#:
// Declare and initialize a 2D array int[,] matrix = { { 1, 2 }, { 3, 4 }, { 5, 6 } }; // Access and print elements of the 2D array Console.WriteLine(matrix[0, 1]); // Output: 2
Passing arrays as parameters in C#:
// Method that takes an array as a parameter static void PrintArray(int[] array) { foreach (int element in array) { Console.WriteLine(element); } } // Declare and initialize an array int[] numbers = { 1, 2, 3, 4, 5 }; // Invoke the method with the array as a parameter PrintArray(numbers);