What's the difference between Tuple.Create() vs new Tuple?

0

Full article

tl;dr - There is none. It's a matter of preference! Keep reading if you want to learn a little something though. πŸ˜‰


When writing a story, it helps to be able to express a concept in a variety of ways, conveying subtle differences... and making the story much more enjoyable.

When writing code, the opposite is true. It's helpful to know exactly how you're supposed to write a solution to a particular problem, and it doesn't help when a language tries to be flexible by providing a dozen ways to do any one thing. I'm looking at you Ruby.

One of the things I like about C# is that there's often one way to do a thing. Even when there are multiple ways, like storing a collection of items, there's usually a best way, like using generics and List<T>. To create a collection that holds any type of object T, you just specify the type - a list of string, integers, your own class - and then you have all kinds of methods available to add and remove items, manipulate them, etc.

var names = new List<string>();

var ages = new List<int>();

class Car
{
    public string Model { get; set; }
    public int Year { get; set; }
    public decimal Cost { get; set; }
}
var cars = new List<Car>();

There's another structure in C# that stores items, called the Tuple. It's sort of like a lightweight class, storing objects of different types that are somewhat related or associated with one another, without the need to define a whole new class. Similar to the List<T> above, you can instantiate it by creating a new Tuple and specifying the types.

var person = new Tuple<string, int>("Bob", 30);

var grade = new Tuple<string, string>("Susan", "B");

Unlike the List<T> though, defining a new Tuple can get annoyingly long:

var randomCollection =
    new Tuple<string, string, decimal, string, Date, bool, string>
        { "John", "Doe", 95.3, "No", DateTime.Now, true, "n/a" };

So the C# team gave us a set of helper methods which remove the need for listing argument types. There's no difference between new Tuple and Tuple.Create under the hood; Tuple.Create() is just a set of overloaded static methods that instantiate new Tuple:

public static class Tuple
{
    public static Tuple<T1> Create<T1>(T1 item1) {
        return new Tuple<T1>(item1);
    }

    public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2) {
        return new Tuple<T1, T2>(item1, item2);
    }

    public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3) {
        return new Tuple<T1, T2, T3>(item1, item2, item3);
    }

    ...

One benefit of this is brevity; another is that you can store anonymous types in a Tuple, for which you otherwise wouldn't be able to say what the type is.

public class Person
{
    public string Name { get; set; }
    public int Height { get; set; }
    public DateTime BirthDate { get; set; }
}

var people = new List<Person>
{
    new Person { Name = "Bob", Height = 72, BirthDate = new DateTime(1984,1,1) },
    new Person { Name = "Mary", Height = 64, BirthDate = new DateTime(1980,2,2) }
};

var oneAnonList = people.Select(x => new { x.Name, x.BirthDate });
var twoAnonList = people.Select(x => new { x.Height, x.Name });

var myTuple = Tuple.Create(oneAnonList, twoAnonList);

There's still not too terribly much you can do with that, since to pass it to another method, you'd still need to be able to define the "type" of the parameter. So it really comes down to convenience and readability, and in both cases the Tuple.Create helper methods win hands-down.

Author

Grant Winney

Is there anything more satisfying than sharing knowledge? Of teaching someone and witnessing their "ah ha" moment? I usually write about tech, but no promises. I hope you find something interesting!



Comments