SoulRayder
SoulRayder

Reputation: 5166

How to create an array of tuples?

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?

Upvotes: 51

Views: 93077

Answers (8)

Rm558
Rm558

Reputation: 4994

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) };
(int X, int Y) [] coords5 = [ (50, 350), (50, 650), (450, 650) ];

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

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

Upvotes: 79

Tom A
Tom A

Reputation: 615

This is what Bing AI just wrote for me. Nice!

prompt: Write a C# method that Creates a series of tuples of doubles named X and Y and adds them to a collection, then converts that collection into two arrays of doubles array-x and array-y .

// Bing says
using System;
using System.Collections.Generic;
using System.Linq;`
 
public class Program
{
    public static void Main()
    {
        // Create a series of tuples of doubles named X and Y
        var tuples = new List<(double X, double Y)>();
        tuples.Add((1.0, 2.0));
        tuples.Add((3.0, 4.0));
        tuples.Add((5.0, 6.0));
        
        // Convert the collection into two arrays of doubles array-x and array-y
        var arrayX = tuples.Select(t => t.X).ToArray();
        var arrayY = tuples.Select(t => t.Y).ToArray();
        
        // Print the arrays for testing
        Console.WriteLine("Array X: " + string.Join(", ", arrayX));
        Console.WriteLine("Array Y: " + string.Join(", ", arrayY));
    }
}

Upvotes: -1

Jo&#227;o Schmidt
Jo&#227;o Schmidt

Reputation: 111

only this

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

Upvotes: 11

Bradley Smith
Bradley Smith

Reputation: 119

As above, you are intending to create a Tuple[] using the notation for List and Dictionary creation, but without constructing a Tuple[]. For all the compiler knows, you could be creating an array of KeyValuePair<int,int>'s or a JSON array, or something else. There is no way to identify the right type to create in your case.

You can get away with it when creating value types because the compiler can identify them and new the objects for you. You get away with it as well when you pass objects in, because the types are identifiable.

Upvotes: 1

Adassko
Adassko

Reputation: 5343

just use:

Tuple<int, int>[] tupleList =
{
    Tuple.Create(1, 2),
    Tuple.Create(2, 3)
};

or even better - if you are holding coordinates as a pair of two integers - there's already a struct for this:

Point[] coordinates =
{
    new Point(1, 2),
    new Point(2, 3)
}

Upvotes: 2

Guru Stron
Guru Stron

Reputation: 141720

instead of using Tuple you can use Point

as for array:

Tuple<int,int>[] aaa = new {Tuple.Create(1, 1),Tuple.Create(1, 1)};

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391306

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),
    ...
};

Upvotes: 43

Akanksha Gaur
Akanksha Gaur

Reputation: 2676

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

Upvotes: 6

Related Questions