Working with Arrays in C#
Arrays are one of the most fundamental collection types in C#. They allow developers to store multiple values of the same data type in a single variable and provide fast access to data through indexes.
Whether you're building desktop applications, web APIs, games, or enterprise software, you'll frequently encounter arrays when working with collections of information.
This guide covers everything you need to know about working with arrays in C#, including creating arrays, accessing values, looping through data, sorting, searching, and performance considerations.
What Is an Array?
An array is a fixed-size collection of elements that all share the same data type. Each element is stored in a specific position known as an index.
int[] numbers =
{
10,
20,
30,
40,
50
};
In this example, the array stores five integer values.
Creating Arrays
Arrays can be declared and initialized in several ways.
string[] colors =
{
"Red",
"Blue",
"Green"
};
You can also specify the size first and assign values later.
int[] scores = new int[3];
scores[0] = 95;
scores[1] = 88;
scores[2] = 91;
Accessing Array Elements
Array elements are accessed using indexes. The first element always has an index of 0.
string[] fruits =
{
"Apple",
"Banana",
"Orange"
};
Console.WriteLine(fruits[0]);
Output
Apple
Modifying Array Elements
Existing values can be updated by assigning a new value to a specific index.
string[] fruits =
{
"Apple",
"Banana",
"Orange"
};
fruits[1] = "Mango";
Console.WriteLine(fruits[1]);
Output
Mango
Finding the Length of an Array
The Length property returns the total number of elements.
int[] numbers =
{
10,
20,
30,
40,
50
};
Console.WriteLine(numbers.Length);
Output
5
Looping Through Arrays
Arrays are commonly processed using loops.
Using a for Loop
string[] fruits =
{
"Apple",
"Banana",
"Orange"
};
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine(fruits[i]);
}
Using a foreach Loop
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
The foreach loop is often easier to read when you only need to access values.
Sorting Arrays
The Array.Sort() method arranges values in ascending order.
int[] numbers =
{
50,
20,
10,
40,
30
};
Array.Sort(numbers);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Output
10
20
30
40
50
Searching Arrays
Use Array.IndexOf() to locate an element.
string[] fruits =
{
"Apple",
"Banana",
"Orange"
};
int index =
Array.IndexOf(fruits, "Banana");
Console.WriteLine(index);
Output
1
Reversing Arrays
Array.Reverse() reverses the order of elements.
int[] numbers =
{
1,
2,
3,
4,
5
};
Array.Reverse(numbers);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Output
5
4
3
2
1
Multidimensional Arrays
C# supports arrays with multiple dimensions.
int[,] matrix =
{
{ 1, 2 },
{ 3, 4 }
};
Console.WriteLine(matrix[1, 0]);
Output
3
Multidimensional arrays are useful for tables, grids, and matrix operations.
Arrays vs Lists
| Feature | Array | List<T> |
|---|---|---|
| Fixed Size | Yes | No |
| Dynamic Growth | No | Yes |
| Performance | Excellent | Very Good |
| Ease of Use | Good | Excellent |
Best Practices
- Use arrays when the collection size is known.
- Validate indexes before accessing elements.
- Use foreach for simple iteration.
- Choose descriptive variable names.
- Consider List<T> for dynamic collections.
Frequently Asked Questions
What is an array in C#?
An array is a fixed-size collection of values that share the same data type.
Are arrays faster than lists?
Arrays generally provide slightly better performance for fixed-size data.
Can arrays grow automatically?
No. Arrays have a fixed size once created.
When should I use a List instead?
Use a List when the number of items may increase or decrease during runtime.
Conclusion
Arrays are a fundamental building block of C# programming and provide an efficient way to store and process collections of data.
Understanding how to create, access, search, sort, and iterate through arrays will help you build stronger programming skills and prepare you for more advanced collection types such as List, Dictionary, and LINQ.