5

I can't find anything about this so I'm not sure if it is possible but I have a tuple that contains the coordinates of an element in a two-dimensional array. I want to be able to find the distance between elements in a two-dimensional array and to do this I want the position of the element in one dimensional array form (I'm not sure of a better way to do this). So is it possible to turn a Tuple into an array?

This is the array:

string[,] keypad = new string[4, 3]
        {
            {"1", "2", "3"},
            {"4", "5", "6"},
            {"7", "8", "9"},
            {".", "0", " "}
        };

This is the method I used to get the coordinates of an element in a multidimensional array:

public static Tuple<int, int> CoordinatesOf<T>(this T[,] matrix, T value)
    {
        int w = matrix.GetLength(0); // width
        int h = matrix.GetLength(1); // height

        for (int x = 0; x < w; ++x)
        {
            for (int y = 0; y < h; ++y)
            {
                if (matrix[x, y].Equals(value))
                    return Tuple.Create(x, y);
            }
        }

        return Tuple.Create(-1, -1);
    }
13
  • 2
    do a google search on C# Convert Tuple into multi-dimensional array stackoverflow.com/questions/13982940/… Commented Sep 5, 2016 at 18:22
  • @MethodMan I want to turn a tuple into an Array not a multidimesnional array Commented Sep 5, 2016 at 18:24
  • 2
    then do the same thing do a google search.. there is an extension method called .ToArray() also are you familiar with linq or lambda expressions..? Commented Sep 5, 2016 at 18:26
  • 2
    how come I can find examples using this in a google search C# stackoverflow convert tuple<int, int> to an array Commented Sep 5, 2016 at 18:28
  • 4
    A) Your question seems to be asking to convert an element of an array into a tuple representation, but your question title is literally the opposite of that. So which one is it? B) You've provided code, but haven't mentioned what part of your current code isn't working. I plugged in your code, and it works how I would expect it to work (except that the x/y coordinates seem switched, but that's a matter of taste). Commented Sep 5, 2016 at 19:05

5 Answers 5

8

In C# 7.0 or above:

var TestTuple =  (123, "apple", 321) ;

object[] values = TestTuple.ToTuple()
                  .GetType()
                  .GetProperties()
                  .Select(property => property.GetValue(TestTuple.ToTuple()))
                  .ToArray();
Sign up to request clarification or add additional context in comments.

Comments

5

If i understand you well, you want to convert Tuple<int, int> into an array...

As i mentioned in the comment to the question, MSDN documentation explains exactly what Tuple<T1, T2> is. A 2-tuple is a pair or KeyValuePair<TKey, TValue> structure...

//create a 2-tuple
Tuple<int, int> t = Tuple.Create(5,11);
//pass Item1 and Item2 to create an array
int[] arr = new int[]{t.Item1, t.Item2};

For further details, please see:
Introduction to Tuples in .NET Framework 4.0
Overview: Working with Immutable Data

2 Comments

Really, you are very good to understand a question so unclear to the point that even sample code has wrong return type!
Thank you. I have feeling that OP wants to convert 2-tuple into an array. ;)
2
ITuple tuple = (1, "2", 3.4);
for (int i = 0; i < tuple.Length; i++)
{
    // use tuple[i] // tuple[i] return object?
}

ITuple Interface (System.Runtime.CompilerServices) | Microsoft Docs

Comments

0
    T[] values = tuple
        .GetType()
        .GetFields()
        .Select(f => f.GetValue(tuple))
        .Cast<T>()
        .ToArray();

should get you an array of Ts (assuming your tuple contains all Ts)!

Comments

0

For both the older Tuple and the newer ValueTuple types, there is a common interface ITuple, which offers the required functionality to handle any possible length of them.

Here is a possible solution for an extension method to convert any tuple to an object array:

public static object[] ToArray(this System.Runtime.CompilerServices.ITuple tuple)
{
    var array = new object[tuple.Length];

    for (var index = 0; index < tuple.Length; index++)
    {
        array[index] = tuple[index];
    }

    return array;
}

Or another solution for an IEnumerable<object> without having to allocate an array:

public static IEnumerable<object> AsEnumerable(this System.Runtime.CompilerServices.ITuple tuple)
{
    for (var index = 0; index < tuple.Length; index++)
        yield return tuple[index];
}

Comments

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.