How to create an array of tuples?

Viewed 67744

I know that to create a tuple in C#, we use the format:

Tuple <int,int>from = new Tuple<int,int>(50,350);
Tuple <int,int>to = new Tuple<int,int>(50,650);

where each tuple is a coordinate pair. I am trying to create an array of multiple coordinate pairs using tuples. Could someone help me out on this?

EDIT: This is what I have tried so far. I want it to be in this array format only.

 Tuple<int, int>[] coords = new Tuple<int,int>({50,350},{50,650},{450,650});

Compiler complains that there is something wrong.. Please tell me what it is?

7 Answers

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}");

only this

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