C# Tutorial: Arrays
Single and multi-dimensional arrays, iterating, sorting, and Array class methods.
Fixed-size collections
Arrays store a fixed number of elements of the same type. The size is set at creation and cannot change.
For dynamic collections, use `List<T>` (next chapter).
What you'll learn in this C# arrays tutorial
This interactive C# tutorial has 8 hands-on exercises. Estimated time: 15 minutes.
- Create an array — Declare an array with `type[] name = new type[size]` or with an initialiser.
- Modify elements — Assign to an index to change an element.
- Iterate with for and foreach — Use `for` when you need the index, `foreach` when you don't.
- Array.Sort and Array.Reverse — Static methods on the `Array` class sort and reverse in place.
- Array methods: Min, Max, Sum — LINQ adds useful array methods via the `using System.Linq` namespace.
- 2D arrays — C# supports rectangular 2D arrays with `[,]` syntax.
- Jagged arrays — Jagged arrays are arrays of arrays, each row having different lengths.
- Array.Copy and spread — `Array.Copy` copies elements between arrays.
C# Arrays concepts covered
- Fixed-size collections