Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an array of tuples?

Tags:

c#

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?

like image 940
SoulRayder Avatar asked Dec 10 '13 09:12

SoulRayder


People also ask

Can you make an array of tuples?

In JavaScript, tuples are created using arrays. In Flow you can create tuples using the [type, type, type] syntax. When you are getting a value from a tuple at a specific index, it will return the type at that index.

How do you create an array of tuples in Java?

Create a tuple class, something like: class Tuple { private Object[] data; public Tuple (Object.. members) { this. data = members; } public void get(int index) { return data[index]; } public int getSize() { ... } }


1 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}"); 
like image 137
Rm558 Avatar answered Sep 24 '22 14:09

Rm558