C# Array
A C# array is a fixed-size, zero-indexed collection of elements of the same data type. Arrays are useful when you know how many related values you need to store and want to access each value by its position.
Every element is accessed with the array name followed by an index in square brackets. The first element is at index 0, the second at index 1, and the last element is at index array.Length - 1.
Consider the following figure, which shows an integer array together with its indexes.

If the array is named myArray, individual values can be read as follows.
myArray[1]gives18.myArray[0]gives25.myArray[3]gives6.
Declare an Array in C#
Declare a one-dimensional array by placing square brackets after the element type.
datatype[] arrayName;
In this syntax:
datatypeis the type of every element stored in the array.arrayNameis the variable used to reference the array.
Examples
int[] numbers;
string[] names;
A declaration creates an array variable, but it does not create an array object. Until an array is assigned, its value is null.
Create a C# Array with a Fixed Length
Create the array object with the new operator and specify the number of elements it can contain.
arrayName = new datatype[size];
In this syntax:
newcreates the array object.datatypespecifies the element type.sizeis the number of elements in the array and must be zero or greater.
Declaration and creation can be combined in one statement.
datatype[] arrayName = new datatype[size];
Examples
int[] numbers = new int[10];
string[] names = new string[6];
After creation, each element contains the default value for its type. For example, elements in an int[] start as 0, elements in a bool[] start as false, and elements in a string[] start as null.
Initialize a C# Array with Values
Initialize an array by listing its values inside curly braces. C# infers the array length from the number of values.
int[] numbers = {25, 18, 87, 6, 41, 54};
string[] names = {"Aby", "Skye", "Mack"};
You can also use an explicit array creation expression when that form is clearer in an assignment or method call.
int[] scores = new int[] { 72, 85, 91 };
string[] cities = new[] { "Delhi", "Pune", "Chennai" };
Access and Update C# Array Elements by Index
Use an index to read or replace an element. Because indexing starts at zero, an array of length 5 has valid indexes from 0 through 4.
string[] colors = { "Red", "Green", "Blue" };
Console.WriteLine(colors[0]);
colors[1] = "Yellow";
Console.WriteLine(colors[1]);
Output
Red
Yellow
C# Integer Array Example
The following program declares and initializes an integer array, then prints selected elements by index.
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
int[] numbers = {25, 18, 87, 6, 41, 54};
Console.WriteLine("numbers[1] : "+numbers[1]);
Console.WriteLine("numbers[3] : "+numbers[3]);
}
}
}
Output
numbers[1] : 18
numbers[3] : 6
C# String Array Example
The following program initializes a string array and prints two values by index.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string[] names = {"Aby", "Skye", "Mack", "Phil", "May"};
Console.WriteLine("names[1] : "+names[1]);
Console.WriteLine("names[3] : "+names[3]);
}
}
}
Output
names[1] : Skye
names[3] : Phil
Get the Length of a C# Array
Use the array’s Length property to get its total number of elements. Length is commonly used as the loop boundary when visiting every element.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string[] names = {"Aby", "Skye", "Mack", "Phil", "May"};
int len = names.Length;
Console.WriteLine("Length of array : "+len);
}
}
}
Output
Length of array : 5
Print a C# Array with a for Loop
Use a C# for loop when the index is needed while processing the array. The condition must use index < names.Length, not index <= names.Length.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string[] names = {"Aby", "Skye", "Mack", "Phil", "May"};
for(int index=0;index<names.Length;index++){
Console.WriteLine(names[index]);
}
}
}
}
Output
Aby
Skye
Mack
Phil
May
Access the Index While Iterating a C# Array
This second C# for loop example uses the same index-based traversal pattern. In practical code, the index can also be printed, compared, or used to update the current element.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string[] names = {"Aby", "Skye", "Mack", "Phil", "May"};
for(int index=0;index<names.Length;index++){
Console.WriteLine(names[index]);
}
}
}
}
Output
Aby
Skye
Mack
Phil
May
Print a C# Array with a while Loop
Use a C# while loop when the loop condition and index update need to be controlled separately.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string[] names = {"Aby", "Skye", "Mack", "Phil", "May"};
int index=0;
while(index<names.Length){
Console.WriteLine(names[index]);
index++;
}
}
}
}
Output
Aby
Skye
Mack
Phil
May
Print a C# Array with foreach
Use C# foreach when you need each value but do not need its index. This is usually the simplest way to read every element.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string[] names = {"Aby", "Skye", "Mack", "Phil", "May"};
foreach(string name in names){
Console.WriteLine(name);
}
}
}
}
Output
Aby
Skye
Mack
Phil
May
Find, Sort, and Reverse Elements in a C# Array
The Array class provides static methods for common array operations. Array.IndexOf finds an element’s index, Array.Sort sorts the array in place, and Array.Reverse reverses its current order.
int[] values = { 40, 10, 30, 20 };
int position = Array.IndexOf(values, 30);
Console.WriteLine($"Index of 30: {position}");
Array.Sort(values);
Console.WriteLine(string.Join(", ", values));
Array.Reverse(values);
Console.WriteLine(string.Join(", ", values));
Output
Index of 30: 2
10, 20, 30, 40
40, 30, 20, 10
Copy a C# Array Without Sharing the Same Storage
Arrays are reference types. Assigning one array variable to another makes both variables refer to the same array. To create a separate shallow copy of a one-dimensional array, use Clone or copy its elements into a new array.
int[] source = { 10, 20, 30 };
int[] copy = (int[])source.Clone();
copy[0] = 99;
Console.WriteLine(source[0]);
Console.WriteLine(copy[0]);
Output
10
99
Handle C# Array Index Errors Safely
Accessing an index below 0 or at least equal to Length throws an IndexOutOfRangeException. Check the index before reading or updating an element.
int[] numbers = { 5, 10, 15 };
int index = 3;
if (index >= 0 && index < numbers.Length)
{
Console.WriteLine(numbers[index]);
}
else
{
Console.WriteLine("Index is outside the array.");
}
Choose Between a C# Array and List<T>
Use an array when the number of elements is fixed or when an API specifically requires an array. Use List<T> when items need to be added or removed dynamically. An array’s Length cannot change after the array is created; resizing requires creating or assigning a different array.
C# Array Questions
What is the first index of an array in C#?
The first index is 0. For an array with n elements, the last valid index is n - 1.
Can a C# array store different data types?
An array has one declared element type. Every assigned value must be compatible with that type. For example, an int[] stores integers, while an object[] can hold values of different runtime types because they are all assignable to object.
Can the length of a C# array change?
No. The length is fixed when the array is created. Use Array.Resize to assign a newly sized array, or use List<T> when the collection needs to grow and shrink frequently.
What happens when a C# array index is invalid?
C# throws an IndexOutOfRangeException when the index is negative or greater than or equal to the array’s Length.
C# Array Tutorial Summary
In this C# Tutorial, you learned how to declare, create, initialize, read, update, and iterate over arrays. You also learned how to use Length, apply common Array methods, copy an array, avoid invalid indexes, and decide when a dynamic List<T> is a better fit.
TutorialKart.com