Arrays are an essential data structure in C#. They allow you to store and manipulate collections of elements efficiently. Whether you’re a beginner learning the basics or an experienced developer looking to reinforce your knowledge, this article covers everything you need to know about arrays in C#. We will explore the fundamentals, array initialization, accessing elements, modifying arrays, multi-dimensional arrays, and more.
What is an Array?
An array is a fixed-size collection of elements of the same type. It provides an efficient way to store and access multiple values under a single variable name. Elements in an array are accessed by their index, which starts from 0.
Array Declaration and Initialization
In C#, arrays can be declared and initialized in several ways. Here are a few examples:
Declaring an array:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration
int[] numbers;
}
}
Initializing an array using the new keyword:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration & initialization
int[] numbers = new int[10];
}
}
Initializing an array with values:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration & initialization
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}
}
Assigning values to individual array indices:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration & initialization
int[] numbers = new int[10];
// Assigning values to individual array indices
numbers[0] = 0;
numbers[1] = 1;
numbers[2] = 2;
numbers[3] = 3;
numbers[4] = 4;
numbers[5] = 5;
numbers[6] = 6;
numbers[7] = 7;
numbers[8] = 8;
numbers[9] = 9;
}
}
Accessing Array Elements
Accessing individual elements in an array is done using their index. The index starts from 0 and goes up to the length of the array minus one. Here’s an example:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration & initialization
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Accessing the first element
int fst = numbers[0];
// Accessing the second element
int snd = numbers[1];
// Accessing the last element
int lst = numbers[9];
// Print the elemnts
Console.WriteLine("First Element: {0}.", fst);
Console.WriteLine("Second Element: {0}.", snd);
Console.WriteLine("Last Element: {0}.", lst);
}
}
Modifying Array Elements
Once an array is initialized, you can modify its elements by assigning new values to specific indices. Here’s an example:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration & initialization
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Modifying the second element
numbers[1] = 10;
}
}
Array Length
The length of an array can be obtained using the Length property. It represents the total number of elements in the array. Here’s an example:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration & initialization
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Get the array length
int length = numbers.Length;
// Print the array length to the console
Console.WriteLine("The numbers array hold {0} elements.", length);
}
}
Iterating over an Array
Arrays are commonly used in loops to perform operations on each element. The foreach loop is particularly useful for iterating over all elements in an array. Here’s an example:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration & initialization
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
foreach (int number in numbers)
{
// Print number to the console
Console.WriteLine(number);
}
}
}
Multi-dimensional Arrays
C# allows you to create multi-dimensional arrays, such as two-dimensional (2D) and three-dimensional (3D) arrays. These are useful for storing data in a grid or matrix-like structure. Here’s an example of a 2D array:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration and initialization (3 rows, 3 columns)
int[,] matrix = new int[3, 3];
}
}
Initializing a multi-dimensional array with values:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration and initialization (3 rows, 3 columns)
int[,] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// Nodifying the value at row 0, column 0
matrix[0, 0] = 1;
// Accessing value at row 1, column 2
int value = matrix[1, 2];
Console.WriteLine("The value at row 1, column 2 is {0}.", value);
}
}
Assigning values to individual array rows, and columns:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration and initialization (3 rows, 3 columns)
int[,] matrix = new int[3, 3];
// 1st row
matrix[0, 0] = 1;
matrix[0, 1] = 2;
matrix[0, 2] = 3;
// 2nd row
matrix[1, 0] = 4;
matrix[1, 1] = 5;
matrix[1, 2] = 6;
// 3rd row
matrix[2, 0] = 7;
matrix[2, 1] = 8;
matrix[2, 2] = 9;
// Accessing the value at row 1, column 2
int value = matrix[1, 2];
Console.WriteLine("The value at row 1, column 2 is {0}.", value);
}
}
Jagged Arrays
Jagged arrays are arrays of arrays. Each element of the array can be an array of a different length. This provides flexibility when dealing with collections of varying sizes. Here’s an example:
using System;
public class App
{
public static void Main(string[] args)
{
// Array declaration: 3 arrays, each with different number of elements
int[][] arrays = new int[3][];
arrays[0] = new int[] { 1, 2, 3 };
arrays[1] = new int[] { 4, 5 };
arrays[2] = new int[] { 6, 7, 8, 9 };
// Accessing the value at row 2, column 3
int value = arrays[2][3];
// Print the value to the console
Console.WriteLine("The value at row 2, column 3 is {0}.", value);
}
}
Conclusion
Arrays are a fundamental concept in C# programming. They provide a convenient way to store and manipulate collections of data. This article covered the basics of arrays, including declaration, initialization, accessing elements, modifying arrays, and working with multi-dimensional and jagged arrays. By mastering the fundamentals of arrays, you’ll have a solid foundation for building more complex programs in C#.
Sources
I hope you found this information informative. If you would like to receive more content, please consider subscribing to our newsletter!