52

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?

4
  • 1
    you could use a List<Tuple<int, int>>... List is a generic collection. Commented Dec 10, 2013 at 9:43
  • Tuple<int,int>[] arrayOftuples=new Tuple<int, int>[]{<tuples here>} Commented Dec 10, 2013 at 9:45
  • If you want only x and y coords than why not use the Point struct. I would at least think really careful about Tuples regarding readability. Commented Dec 10, 2013 at 10:24
  • The reason is I am using a built-in function which accepts the Tuple array as a parameter. Can't change that :-/ Commented Dec 10, 2013 at 10:28

8 Answers 8

80

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}");
Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer for C# 7 and beyond. If you want named items you can do that as well: var myTupleArray = new (double myFirstItem, int mySecondItem)[] = { (12.0, 2), (13.0, 3), (14.0, 4)};
In case you want to use the name also when giving the values: (int X, int Y)[] coords = { (X: 50, Y: 350), (X: 50, Y: 650), (X: 450, Y: 650) };
43

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

1 Comment

This, it seems like a lot of needless effort to work with tuples in a case such as this.
11

only this

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

3 Comments

as a note for future answers - you'll receive better upvotes for the following: - 1. timeliness of response (unless the answer provides something new) 2. Explanation of answer (providing descriptions allows the OP and readers to understand why the answer is worthwhile)
In addition to @DenisTsoi 's comment - which I agree with. I would also make a note that deconstruction in C# is only available from C#7.0.
Upvote for modern tuples. I wish more people knew about them. Yes, you have to have C# 7.0, but many do now. I agree that more substance to your answer would help a lot, though.
7

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

Comments

2

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

Comments

1

instead of using Tuple you can use Point

as for array:

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

Comments

1

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.

Comments

-1

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

1 Comment

I'm not sure why someone downvoted this. I think my answer provides a few points the accepted answer does not. 1) Shows that Bing AI is actually a great productivity tool for programmers -- like StackOverflow. 2) Pulls the tuples out into arrays in an elegant fashion. This was not part of OP's question, but I am writing test data for an API that accepts two arrays, and I found this to be a clean solution -- I can write test data in a clear tuple fashion, and then pass the arrays into the API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.