Tuples & deconstruction in C#
Group values without a class, pull them apart, and swap in one line.
var point = (X: 3.0, Y: 4.0);
Console.WriteLine($"x={point.X}, y={point.Y}");
// deconstruct into locals, or swap two values without a temp
var (min, max) = (10, 5);
(min, max) = (max, min);
Console.WriteLine($"min {min}, max {max}");
(string name, int age) person = ("Ada", 36);
Console.WriteLine($"{person.name} is {person.age}");
How it works
(X: 3.0, Y: 4.0)builds a tuple with named elements.var (min, max)deconstructs a tuple into fresh locals.- Assigning
(max, min)swaps the pair without a temporary.
Keywords and builtins used here
intstringvar
The run, in numbers
- Lines
- 10
- Characters to type
- 334
- Tokens
- 75
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 50 seconds.
Step 3 of 3 in Variables & types, step 3 of 29 in Language basics.