Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Arrays in C#

What is an Array?

An array is a data structure that contains a collection of elements. These elements are stored in contiguous memory locations. The elements can be of any data type, and each element can be accessed using its index. In C#, arrays are zero-based, meaning the first element is at index 0.

Declaring an Array

To declare an array in C#, you must specify the type of its elements and the number of elements it will hold. The following is the syntax for declaring an array:

dataType[] arrayName = new dataType[arraySize];

For example, to declare an array of integers with 5 elements:

int[] numbers = new int[5];

Initializing an Array

Once an array is declared, it can be initialized with values. Initialization can be done at the time of declaration or later in the program. Here are the two methods:

1. Initialization at the time of declaration:

int[] numbers = new int[] {1, 2, 3, 4, 5};

2. Initialization after declaration:

int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;

Accessing Array Elements

You can access the elements of an array using their index. The index is zero-based, so the first element is at index 0, the second element is at index 1, and so on. Here's an example:

int[] numbers = {1, 2, 3, 4, 5};
int firstNumber = numbers[0]; // 1
int secondNumber = numbers[1]; // 2

Looping Through an Array

You can use loops to iterate through the elements of an array. The most common loops used are the for loop and the foreach loop. Below are examples of both:

Using a for loop:

int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.Length; i++)
{
    Console.WriteLine(numbers[i]);
}

Using a foreach loop:

int[] numbers = {1, 2, 3, 4, 5};
foreach (int number in numbers)
{
    Console.WriteLine(number);
}

Multidimensional Arrays

C# supports multidimensional arrays. The most common are two-dimensional arrays, which can be thought of as a table with rows and columns. Here is an example of declaring and initializing a two-dimensional array:

int[,] matrix = new int[3, 3];
matrix[0, 0] = 1;
matrix[0, 1] = 2;
matrix[0, 2] = 3;
matrix[1, 0] = 4;
matrix[1, 1] = 5;
matrix[1, 2] = 6;
matrix[2, 0] = 7;
matrix[2, 1] = 8;
matrix[2, 2] = 9;

You can access the elements of a two-dimensional array using two indices, one for the row and one for the column:

int value = matrix[1, 1]; // 5

Jagged Arrays

A jagged array is an array of arrays, where each inner array can have a different length. Here is an example of declaring and initializing a jagged array:

int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] {1, 2};
jaggedArray[1] = new int[] {3, 4, 5};
jaggedArray[2] = new int[] {6, 7, 8, 9};

You can access the elements of a jagged array using two indices:

int value = jaggedArray[1][2]; // 5

Common Array Methods

Arrays in C# come with a variety of built-in methods that simplify many common operations. Some of these methods include:

  • Array.Sort(array): Sorts the elements in an array.
  • Array.Reverse(array): Reverses the order of the elements in an array.
  • Array.IndexOf(array, value): Returns the index of the first occurrence of a value in an array.
  • Array.Resize(ref array, newSize): Changes the size of an array.

Here is an example of using some of these methods:

int[] numbers = {5, 3, 8, 1, 2};
Array.Sort(numbers); // {1, 2, 3, 5, 8}
Array.Reverse(numbers); // {8, 5, 3, 2, 1}
int index = Array.IndexOf(numbers, 3); // 2
Array.Resize(ref numbers, 7); // {8, 5, 3, 2, 1, 0, 0}

Conclusion

Arrays are a fundamental aspect of C# programming and provide a simple yet powerful way to store and manipulate collections of data. Understanding arrays and their various types and methods will enhance your ability to write efficient and effective code.