Any tips to make working with Tuples easier in C#?

Often you want to send multiple values but due to low use (i.e. it is only used in one or two places), it's hard to justify creating a new type.

The Tuple<...> and KeyValuePair type are very useful, but there isn't real language support for them.

Well sort of, a nice trick to use for Lists of tuples is to create a type that extends the List and adding a custom add method: e.g.

public class TupleList : List>{
    public void Add(T1 key, T2 value){
        base.Add(Tuple.Create(key, value));
    }
}

This means that if I have a method that takes an IEnumerable>, I can use the following code to quickly build up the list like so::

Foo(new TupleList{{1,"one"},{2,"two"},{3,"three"}});

This makes winding values into a tuple list easier as we don't have to constantly keep saying Tuple.Create, and gets us almost to a nice functional languages syntax.

But when working with a tuple it is useful to unwind it out into its different components. This extension method might be useful in this respect::

    public static void Unwind(this Tuple tuple,out T1 var1,out T2 var2)
    {
        var1 = tuple.Item1;
        var2 = tuple.Item2;
    }

But even that's annoying as out parameters are not variant at all. That is if T1 is a string, I can't send in an object variable even though they are assignable, when as I can do the unwinding by hand otherwise. I can't really suggest a reason why you might want this variance, but if its there, I can't see why you would want to lose it.

Anyone have other tips to making working tuples, or tuple like objects easier in C#?

An important potential use for tuples might be generic memoization. Which is very easy in languages like F#, but hard in C#.

I'm currently using Tuples to supply a MethodBase and an array of tokens (constants, objects, or argument tokens), supplied to a dynamicly built object to construct certain member fields.

Since I wanted to make the syntax easier on API consumers, I created Add methods that can take a ConstructorInfo or a MethodInfo and a params array of objects.

Edit: У Эрика Липперта, как обычно, отличная мотивация для использования здесь кортежей, и он даже говорит то, что, как я подозревал, на самом деле нет поддержки: What requirement was the tuple designed to solve?

10
задан Community 23 May 2017 в 11:51
поделиться