Tech Off Post

Single Post Permalink

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

    exoteric said:

    I'll start out -

     

    Tuple Syntax 
    a la (precise parenthesization not so important here)

    (1,2,3)

    Tuple Enumeration

    Not a language feature but allow tuple to be enumerated (IEnumerable<T>) for both homogeneous and heterogeneous tuples, meaning these two cases would both be legal

    foreach (var x in (1,2,3))
      Console.WriteLine(x);
    foreach (var x in (1,"WTF",3))
      Console.WriteLine(x);

    Where in the first case the tuple would be IEnumerable<int> and in the second case the tuple would be either IEnumerable<IDynamic> or IEnumerable<Object>.

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