Syntax
type[] arrayName = new type[size];Examples
Creating and Accessing Arrays
Declaring arrays with initial values and reading elements by index.
int[] numbers = { 10, 20, 30, 40, 50 };
string[] names = new string[3];
names[0] = "Fola";
names[1] = "Zain";
names[2] = "Jamal";
Console.WriteLine(numbers[0]); // 10
Console.WriteLine(numbers.Length); // 5
Console.WriteLine(names[1]); // ZainIterating Over an Array
Looping through array elements with a standard for loop or a foreach loop.
int[] scores = { 85, 92, 78, 90 };
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine($"Index {i}: {scores[i]}");
}
// foreach - simpler when you don't need the index
foreach (int score in scores)
{
Console.WriteLine(score);
}Multi-Dimensional and Jagged Arrays
A true 2D grid versus an array of arrays with potentially different lengths.
int[,] grid = {
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Console.WriteLine(grid[1, 2]); // 6 (row 1, column 2)
int[][] jagged = new int[2][];
jagged[0] = new int[] { 1, 2, 3 };
jagged[1] = new int[] { 4, 5 }; // different length - that's allowed
Console.WriteLine(jagged[1][1]); // 5Common Array Operations with System.Array
Using the built-in Array class for sorting, reversing, and searching.
int[] numbers = { 5, 2, 8, 1, 9 };
Array.Sort(numbers);
Console.WriteLine(string.Join(", ", numbers)); // 1, 2, 5, 8, 9
Array.Reverse(numbers);
Console.WriteLine(string.Join(", ", numbers)); // 9, 8, 5, 2, 1
int index = Array.IndexOf(numbers, 5);
Console.WriteLine(index); // 2Best practices
- Use a List<T> instead of a plain array whenever the collection needs to grow or shrink dynamically
- Use foreach when you just need the values, and a standard indexed for loop when you need the index too
- Use string.Join() to print array contents readably - printing an array directly with WriteLine() shows its type name, not its contents
- Prefer jagged arrays (int[][]) over true multi-dimensional arrays (int[,]) when rows might have different lengths - jagged arrays are also generally faster in C#
At a glance
- Purpose
- Applications on the .NET platform
- File extension
- .cs
- Runs in
- .NET runtime
- Usually used with
- .NET SDK and libraries
Specifications & further reading
Related C# documentation
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.