Arrays in C#
Fixed-size, typed, and sliceable with index-from-end and range syntax.
int[] fib = { 1, 1, 2, 3, 5, 8, 13 };
Console.WriteLine($"{fib.Length} values, third {fib[2]}");
// ^ counts from the end, .. takes a slice
Console.WriteLine(fib[^1]);
int[] middle = fib[2..5];
Console.WriteLine(string.Join(",", middle));
var grid = new int[2, 3];
grid[1, 2] = 9;
Console.WriteLine(grid[1, 2]);
How it works
{ 1, 1, 2, ... }initializes;Lengthand[2]read.[^1]indexes from the end;[2..5]copies a slice.new int[2, 3]is a true rectangular array, one block of memory.
Keywords and builtins used here
intnewstringvar
The run, in numbers
- Lines
- 11
- Characters to type
- 313
- Tokens
- 98
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 65 seconds.
Step 1 of 4 in Collections, step 11 of 29 in Language basics.