4

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);
    }
user3124306
  • 685
  • 1
  • 9
  • 20
  • 2
    do a google search on `C# Convert Tuple into multi-dimensional array` http://stackoverflow.com/questions/13982940/list-of-tuples-to-multi-dimensional-array – MethodMan Sep 05 '16 at 18:22
  • @MethodMan I want to turn a tuple into an Array not a multidimesnional array – user3124306 Sep 05 '16 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..? – MethodMan Sep 05 '16 at 18:26
  • @MethodMan I did, but I couldn't find anything so that is why I asked here – user3124306 Sep 05 '16 at 18:27
  • 2
    how come I can find examples using this in a google search `C# stackoverflow convert tuple to an array` – MethodMan Sep 05 '16 at 18:28
  • @MethodMan I can't quite find what I'm talking about. The closest I found was turning tuple into an array, but this isn't what I'm looking for. I am a beginner at C# so I don't really know how to implement this. If you have a link to the question I'm asking then please post it. If you were the person that downvoted this question then please undo that if you realize that I really couldn't find what I was looking for. – user3124306 Sep 05 '16 at 18:33
  • Take a look at [MSDN documentation](https://msdn.microsoft.com/en-us/library/dd268536(v=vs.110).aspx) – Maciej Los Sep 05 '16 at 18:36
  • @MethodMan Sorry, but this is still not what I'm looking for. I want to turn a tuple into an array like the title of my question says? – user3124306 Sep 05 '16 at 18:39
  • 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). – Kolichikov Sep 05 '16 at 19:05
  • @Kolichikov My code is working, but it gives a Tuple, I want the elements in the tuple to be in a normal one-dimensional array. If I'm missing something extremely obvious here, please tell me, I stress that I am a beginner – user3124306 Sep 05 '16 at 19:20
  • You should really expect down votes because **your question is very unclear**. Show sample data and desired data types. You have to make a lot more effort to don't get down votes. – Phil1970 Sep 05 '16 at 19:42
  • It does not matter that you are a beginner. You have to ask good question that one can easily understand by reading the question only once. Your question talk about converting an `Tuple` into an array but the function you wrote take an array and return a `Tuple` which is exactly the opposite. Also, why on earth would you create a `Tuple` if you want to return an array. This make no sense to do that. So you have something simple to do and you make it complicate. – Phil1970 Sep 05 '16 at 20:00
  • possible related question: https://stackoverflow.com/q/44007004 – NoonKnight Nov 12 '21 at 16:41

4 Answers4

6

In C# 7.0 or above:

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

object[] values = TestTuple.ToTuple()
                  .GetType()
                  .GetProperties()
                  .Select(property => property.GetValue(TestTuple.ToTuple()))
                  .ToArray();
Steven Chou
  • 1,504
  • 2
  • 21
  • 43
3

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

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
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

CWKSC
  • 196
  • 1
  • 11
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)!