How to create an array of tuples?

You can create an array like this. You will need to initialize each of the pairs.

Tuple<int, int>[] tupleArray = new Tuple<int, int>[10];

tupleArray[0] = new Tuple<int, int>(10,10);

only this

(int x, int y)[] coords = new (int, int)[] {(1, 3), (5, 1), (8, 9)};

in C# 7

var coords = new[] { ( 50, 350 ), ( 50, 650 ), ( 450, 650 )};

enter image description here

for named version, do this: (thanks entiat)

var coords2 = new(int X, int Y) [] { (50, 350), (50, 650), (450, 650) };

or

(int X, int Y) [] coords3 = new [] { (50, 350), (50, 650), (450, 650) };

or

(int X, int Y) [] coords4 = { (50, 350), (50, 650), (450, 650) };

so we can use X, Y instead of Item1, Item2

coords4.Select(t => $"Area = {t.X * t.Y}");

You can define it as follows:

Tuple<int, int>[] tuples =
{
    Tuple.Create(50, 350),
    Tuple.Create(50, 650),
    ...
};

Though if this is coordinate values, I'd probably use Point instead:

Point[] points =
{
    new Point(50, 350),
    new Point(50, 650),
    ...
};

Tags:

C#