In C#, an array stores a fixed number of elements of the same type in one block of memory. You write int[] scores = { 90, 85, 70 }; or new int[5]. Indexes start at 0. The length is fixed for that array object—you cannot Add a sixth slot without allocating a new array.
The type System.Array is the base for all arrays. It offers helpers such as Sort(), Find(), IndexOf(), and Copy(). Many calls are static: Array.Sort(arr).
This page teaches what a C# array is. The Array Methods index lists every method guide in this series.
Need a growing collection?
Use List<T> when the count changes while the program runs. Convert with list.ToArray() when an API needs an array.
Syntax
C# Syntax
Common ways to create and use arrays:
C#
int[] a = { 1, 2, 3, 4, 5 }; // five ints
int[] zeros = new int[5]; // five zeros
int n = a.Length; // 5
int x = a[0]; // first element
Array.Sort(a); // sort in place
int ix = Array.IndexOf(a, 3); // index or -1
Reference
Array vs List<T>
The two are easy to mix up. Here is a quick comparison:
Feature
int[]
List<int>
Length / Count
Fixed after create
Can grow and shrink
Add / Remove
No (use Resize or new array)
Yes
Indexing
a[i]
list[i]
Helpers
Array.Sort, Array.Find, …
Add, Find, LINQ, …
Best for
Fixed buffers, simple APIs
Changing collections
Code
C# Examples
Three short programs that show how arrays work with System.Array helpers.
Example 1 — Create, Length, Sort, and IndexOf
A first program that uses common Array helpers.
C#
using System;
class Program {
static void Main() {
int[] nums = { 40, 10, 30, 20 };
Console.WriteLine("Length: " + nums.Length);
Array.Sort(nums);
Console.WriteLine("After Sort: " + string.Join(", ", nums));
int ix = Array.IndexOf(nums, 30);
Console.WriteLine("Index of 30: " + ix);
}
}
Output
Length: 4
After Sort: 10, 20, 30, 40
Index of 30: 2
How It Works
Fixed length.nums holds four ints; Length reports 4 and never changes for this object.
Sort in place.Array.Sort(nums) reorders the same array to ascending order.
Find an index.IndexOf returns 2 for value 30, or -1 when missing.
Example 2 — Find the First Match with a Predicate
A predicate is a tiny yes/no test. Array.Find returns the first element that passes.
C#
using System;
class Program {
static void Main() {
string[] names = { "Ann", "Bob", "Carla", "Dan" };
string hit = Array.Find(names, n => n.Length > 3);
Console.WriteLine(hit ?? "(none)");
}
}
Output
Carla
How It Works
Lambda test.n => n.Length > 3 is true only for longer names.
First win.Find stops at "Carla" and does not keep scanning.
Missing value. If nothing matches, reference types return null (the default).
Example 3 — Copy Elements and Resize with ref
Copy fills another array. Resize replaces your variable with a new array of a different length.
C#
using System;
class Program {
static void Main() {
int[] src = { 1, 2, 3 };
int[] dest = new int[5];
Array.Copy(src, dest, src.Length);
Console.WriteLine("dest: " + string.Join(", ", dest));
Array.Resize(ref src, 5);
Console.WriteLine("src Length: " + src.Length);
Console.WriteLine("src: " + string.Join(", ", src));
}
}
An array is a fixed-length collection of elements of one type, such as int[] or string[]. Indexes start at 0. After you create it, Length does not change for that object.
Arrays have a fixed length. List<T> can grow with Add and shrink with Remove. Prefer List when the count changes often; prefer arrays for fixed buffers and simple APIs.
Static methods use Array.Name(...), for example Array.Sort(arr) and Array.Find(arr, predicate). Instance methods use a dot on the array, for example arr.Clone() and arr.GetLength(0).
Yes. Sort first (or build the array already ordered). Searching an unsorted array gives undefined results.
Open the C# Array Methods index. It lists Sort, Find, IndexOf, Copy, Resize, GetLength, and the rest, each with examples and FAQs.