Tech Off Post

Single Post Permalink

View Thread: Ideas for C# 5.0
  • User profile image
    exoteric

    TommyCarlier said:
    exoteric said:
    *snip*

    I would be against tuple enumeration. A tuple is not a collection and should not be considered as such. If you really want that feature, you could build it yourself:

    public static class TupleExtensions
    {
      public static IEnumerable<T> ToCollection<T>(this Tuple<T, T> tuple)
      {
        yield return tuple.Item1;
        yield return tuple.Item2;
      }
      
      public static IEnumerable<object> ToCollection<T1, T2>(this Tuple<T1, T2> tuple)
      {
        yield return tuple.Item1;
        yield return tuple.Item2;
      }
    
      public static IEnumerable<T> ToCollection<T>(this Tuple<T, T, T> tuple)
      {
        yield return tuple.Item1;
        yield return tuple.Item2;
        yield return tuple.Item3;
      }
      
      public static IEnumerable<object> ToCollection<T1, T2, T3>(this Tuple<T1, T2, T3> tuple)
      {
        yield return tuple.Item1;
        yield return tuple.Item2;
        yield return tuple.Item3;
      }
      
      ...
    }

    A tuple is a collection. It's just a heterogeneous collection where elements do not always have the same type. That said, using extension methods for tuple enumeration could be a pretty good midpoint between no support and built-in support.