Arrays in C# are one of the most fundamental ways to store multiple values of the same type in a fixed-size collection. If you need to hold several integers, strings, objects, or custom values under one variable name and access them by index, arrays are the starting point.
An array is fast, direct, and predictable. It stores elements in order, gives each element an index, and keeps its length fixed after creation. Because of that design, arrays are used heavily in algorithms, loops, mathematical operations, buffer handling, image processing, game development, and internal framework logic.
Even when you later use collections such as List<T>, Dictionary<TKey, TValue>, or LINQ queries, understanding arrays remains important. Many higher-level APIs still depend on array concepts such as indexing, contiguous storage, copying, slicing, and iteration.
What Is an Array in C#?
An array is a fixed-size collection of elements of the same data type. Every element is stored in a specific position, and that position is accessed by using a zero-based index.
int[] numbers = new int[5];
This declaration creates an array that can hold exactly five integers. The indexes are 0, 1, 2, 3, and 4.
Syntax of Array in C#
dataType[] arrayName;
After declaration, the array is usually created with the new keyword.
dataType[] arrayName = new dataType[size];
The square brackets indicate that the variable stores an array, and the size determines how many elements it can contain.
Declaring and Initializing Arrays
You can initialize an array in more than one way depending on whether you already know the values.
int[] marks = new int[3];
int[] scores = new int[] { 85, 90, 95 };
string[] names = { "Aarav", "Diya", "Kabir" };
The third form is a shorthand syntax that the compiler understands automatically. It is common when the values are already known at the time of declaration.
Accessing Array Elements
Each array element is accessed by its index. Since C# arrays are zero-based, the first element uses index 0.
int[] numbers = { 10, 20, 30, 40 };
Console.WriteLine(numbers[0]); // 10
Console.WriteLine(numbers[2]); // 30
numbers[1] = 99;
Console.WriteLine(numbers[1]); // 99
You can both read and update values by index as long as the index stays within range.
Default Values in Arrays
When you create an array with a size but do not assign explicit values, each element receives the default value of its type.
| Type | Default Value |
|---|---|
| int | 0 |
| double | 0 |
| bool | false |
| char | \0 |
| string | null |
| reference type | null |
int[] values = new int[4];
Console.WriteLine(values[0]); // 0
This behavior is useful, but it also means you should not assume uninitialized elements contain meaningful data.
Iterating Through an Array
Arrays are commonly processed with loops. The two most common choices are for and foreach.
int[] numbers = { 5, 10, 15, 20 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Use for when you need the index. Use foreach when you only need the values and want simpler, safer iteration.
Array Length in C#
The Length property returns the total number of elements in the array.
string[] fruits = { "Apple", "Banana", "Mango" };
Console.WriteLine(fruits.Length); // 3
This property is heavily used in loops to avoid hardcoding the size.
Multidimensional Arrays in C#
A multidimensional array stores values in rows and columns or in higher dimensions. The most common example is a two-dimensional array.
int[,] matrix =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Console.WriteLine(matrix[1, 2]); // 6
This is useful for tabular data, grids, matrices, and board-like structures. Each element is accessed with multiple indexes.
Jagged Arrays in C#
A jagged array is an array of arrays. Each row can have a different length, which makes it more flexible than a rectangular multidimensional array.
int[][] jagged = new int[3][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };
jagged[2] = new int[] { 6 };
In a jagged array, the outer array stores references to inner arrays. This is useful when rows naturally have different sizes, such as grouped records or triangle-like data structures.
Useful Array Methods and Properties
The Array class provides many helper methods for sorting, searching, copying, resizing, and clearing arrays.
| Member | Purpose |
|---|---|
Length | Gets total number of elements |
Array.Sort() | Sorts the array |
Array.Reverse() | Reverses element order |
Array.IndexOf() | Finds the index of an element |
Array.Copy() | Copies elements between arrays |
Array.Clear() | Resets elements to default values |
Array.Resize() | Changes array variable to a new sized array |
int[] values = { 40, 10, 30, 20 };
Array.Sort(values);
Array.Reverse(values);
int index = Array.IndexOf(values, 30);
Console.WriteLine(index);
These helpers remove the need to write many low-level operations manually.
Passing Arrays to Methods
When you pass an array to a method, the method receives access to the same array object. That means changing an element inside the method changes the original array contents.
static void UpdateFirstElement(int[] data)
{
data[0] = 500;
}
int[] numbers = { 1, 2, 3 };
UpdateFirstElement(numbers);
Console.WriteLine(numbers[0]); // 500
This behavior matters because arrays are reference types even though the elements inside them may be value types. You should know whether your method is expected to modify the caller’s data.
Arrays of Strings and Objects
Arrays are not limited to primitive values. You can store strings, objects, and even custom types inside them.
string[] cities = { "Delhi", "Mumbai", "Pune" };
Student[] students = new Student[2];
students[0] = new Student { Name = "Aarav" };
students[1] = new Student { Name = "Diya" };
When the array element type is a reference type, each element stores a reference to an object. Empty positions hold null until assigned.
Array vs List in C#
| Point | Array | List<T> |
|---|---|---|
| Size | Fixed after creation | Dynamic |
| Index access | Fast | Fast |
| Add and remove convenience | Limited | High |
| Memory overhead | Usually lower | Usually higher than array |
| Best use case | Known size and direct storage | Flexible growing collections |
Choose an array when the size is known or fixed. Choose List<T> when the number of elements changes frequently during program execution.
Common Mistakes with Arrays
- Using an index outside the valid range and causing
IndexOutOfRangeException. - Forgetting that arrays are zero-based.
- Expecting an array to grow automatically after creation.
- Assuming unassigned reference-type elements are valid objects instead of
null. - Using
foreachwhen element modification by index is required.
Most beginner bugs with arrays come from indexes, size assumptions, or confusion between arrays and dynamic collections.
Best Practices for Arrays in C#
- Use arrays when the size is fixed or known in advance.
- Prefer
array.Lengthover hardcoded size values. - Validate indexes before accessing when input may be unsafe.
- Use
foreachfor read-only iteration andforwhen indexes matter. - Switch to
List<T>when collection size must grow or shrink often.
Arrays Interview Points
For interviews, remember that arrays in C# are fixed-size, zero-based, and type-safe. They are reference types, but they can store either value-type elements or reference-type elements. Common interview comparisons include array vs list, multidimensional array vs jagged array, and for vs foreach traversal.
You should also remember the default values of uninitialized elements, the role of the Length property, and the fact that passing an array to a method allows the method to modify its contents.
FAQs on Arrays in C#
What is an array in C#?
An array is a fixed-size collection of elements of the same type. Each element is accessed by a zero-based index.
Are arrays fixed-size in C#?
Yes. Once created, an array does not grow automatically. If you need dynamic size, use List<T> or create a new array.
What is the difference between multidimensional and jagged arrays?
A multidimensional array uses a rectangular layout like rows and columns. A jagged array is an array of arrays where each inner array can have a different length.
How do I get the size of an array in C#?
Use the Length property. It returns the total number of elements in the array.
Copying Arrays in C#
If you assign one array variable to another, both variables point to the same array object. That means changes through one variable are visible through the other. This is different from copying all elements into a new array.
int[] first = { 1, 2, 3 };
int[] second = first;
second[0] = 99;
Console.WriteLine(first[0]); // 99
If you need a separate copy, use helpers such as Array.Copy(), Clone(), or create a new array and copy the values manually. This point matters in debugging because shared array references can cause unexpected updates.
Choosing Between Single, Multidimensional, and Jagged Arrays
A single-dimensional array is the most common choice for ordered lists of values. A multidimensional array works well when data is naturally rectangular, such as rows and columns in a matrix. A jagged array is useful when each row can have a different length.
The right choice depends on the shape of the problem. If every row has the same number of elements, a multidimensional array can be clearer. If rows vary in size, a jagged array is usually more flexible and easier to manage.
Arrays in Performance-Sensitive Code
Arrays are common in performance-sensitive code because they are simple, compact, and index-based. In parsers, numeric algorithms, game loops, and buffer processing, arrays often provide predictable access patterns and low overhead compared with more feature-rich collections.
That does not mean arrays are always the best answer. It means they remain an important tool when control over layout, iteration, and fixed-size storage matters.
For that reason, strong array fundamentals continue to matter even after you start using higher-level generic collections in day-to-day C# work.