Skip to content

EN CSharp Arrays

Bad Newbie edited this page Oct 16, 2026 · 3 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Arrays, Indexes and Ranges

Manual C# · Examples UO

An array has a fixed length when created and stores elements of the same type. The index starts from zero. Length returns the number of elements. A jagged array contains other arrays; multidimensional uses multiple indexes.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
int[] counts = [2, 3, 4];
System.Console.WriteLine(counts[0]);
System.Console.WriteLine(counts[^1]);

Result:

2
4
int[] values = [10, 20, 30, 40];
int last = values[^1];
int[] middle = values[1..3];
UO.Print($"Last={last}, middle={string.Join(",", middle)}");
int[,] grid = new int[2, 3];
grid[1, 2] = 7;
int[][] groups = [[1, 2], [3]];
UO.Print($"Grid={grid[1, 2]}, group={groups[0].Length}");

^1 denotes the last element, the range of start..end does not include end. For arrays with unknown content, check the length first, then access the element. Creating an array range creates a new array.

Official Documentation: Arrays, Indexes, and Ranges - Microsoft Learn.

Clone this wiki locally