Search code examples
c#arraystuples

Convert array to tuple?


Is it possible to convert array to tuple in C#? Something like this:

var ar = new int[2] {5, 7};
Tuple<int,int> t = Tuple.Create(ar);

Solution

  • No, System.Tuple has a maximum size for good reason. It's simply the wrong tool for the job you appear to be doing. Why don't you just return the array instead of a tuple? Your approach could end up needing a tuple with dozens of elements which is beyond ridiculous and not at all maintainable.

    Even better instead of returning the array, return an interface such as ICollection<T> or IEnumerable<T>.