A C# library to write functional code – Part II – Tuples

Other posts in the series:

  • Part I – Background
  • Part II – Tuples
  • Part III – Records

  • Part IV – Type Unions

  • Part V – The Match operator

  • Tuples are a way for you not to name things. In Object Oriented languages you got to name everything. If you need to represent a bunch of data, you create a class for it.

    There is a strange asymmetry in mainstream OO languages in that you can pass multiple parameters to a function, but you can return just one value. Granted, there are ways around it: you can use ‘ref’ in C# or return some sort of collection where things are stored. But by and large the model is: you pass many, you get one; if you need to return more than one, create a class to represent this ‘bunch of data’. Tuples are a way for you not to create such a class.

    Tuples are also much more than that. Once you have the language concept of ‘a bunch of data without a name’, you can create arrays of them, you can pass them as parameters, use them as local variables. Wherever you’d use a type, you can use a Tuple instead.

    This is particularly appealing to me as I like to use classes almost exclusively to represent things that have a counterpart in the domain I’m modeling (i.e. Customer, Account). I don’t like to create classes/structs just for the sake of temporarily put some data together.

    You can create your own Tuple class in C#, but the syntax gets ugly. Syntax matter. Syntax helps you to think differently about your program. We have syntax for anonymous types, but given that they cannot escape the scope of a method, they cannot be used as full replacement for Tuples.

    In any case, to my implementation. Here is how you create a Tuple:

                var t1 = F.Tuple(34, “bo”, 2.3);

    not too bad. In F# it is better: (34, “bo”, 2.3). And you often don’t need the parenthesis. But still, my C# version is ok.

    You then need to access its elements:

                var n = t1.Item1;
    var s = t1.Item2;

    In F# you usually access them by doing pattern matching, which gives a more intuitive syntax. But again, my C# syntax is not terrible. 

    Tuples need to have structural equality, which means that the following has to work:

            ArrayList mad1 = new ArrayList { new List<IEnumerable<string>> { new string[] { “bo” }, new string[] { “bo” } },
    32, “bo”, new int[] { 4, 5, 6 } };
    ArrayList mad2 = new ArrayList { new List<IEnumerable<string>> { new string[] { “bo” }, new string[] { “bo” } },
    32, “bo”, new int[] { 4, 5, 6 } };
    ArrayList mad3 = new ArrayList { new List<IEnumerable<string>> { new string[] { “bo” }, new string[] { “bo” } },
    32, “bo”, new int[] { 4, 5, 5 } };
            Assert.AreEqual(F.Tuple(mad1, mad2, mad1), F.Tuple(mad2, mad1, mad2));
    Assert.AreNotEqual(F.Tuple(mad1, mad2, mad1), F.Tuple(mad1, mad3, mad1));

    You can use Tuples as return values, parameters, locals etc. Unfortunately, the syntax is ugly when Tuples are part of the signature of a function:

        public Tuple<string, IEnumerable<Tuple<string, ObservationHistory>>> Execute() {


    With the above information, you can be a user of Tuples. From this point on, I’ll talk about some details of the implementation (I also attach the full code to this post as a zip file).

        public class Tuple<T1> {

    public Tuple(T1 t1) {

    Item1 = t1;

    public readonly T1 Item1;

    #region Equals, GetHashCode, ==, !=

    public class Tuple<T1, T2> : Tuple<T1> {

    public Tuple(T1 t1, T2 t2) : base(t1) { Item2 = t2; }

    public readonly T2 Item2;

    #region Equals, GetHashCode, ==, !=


    So, Tuples are classes, not structs. The reason for it is fully described in this series of posts. They also inherit from one another. There are pros and cons to that. The main pros are that I had to write less code and that you can pass a Tuple<int, string> when a function expects a Tuple<int, string, int>. The main drawback is that you can pass a Tuple<int, string> when a function expects a Tuple<int, string, int>.  Also notice the use of public fields. These is a problem with frameworks that insist on properties (i.e. Data Binding). Also, I just got to 5 as arity goes. The day I need 6 items, I’ll add another one. It is boilerplate code (that I’d still like not to write).

    The Equals method is a bit convoluted:

        internal static class Utils {

    public static void CheckNull<T>(T t) {

    if (t == null)
    throw new ArgumentNullException();

           public override bool Equals(object right) {


    if (object.ReferenceEquals(this, right))
    return true;

    if (this.GetType() != right.GetType())
    return false;

    var rightT = right as Tuple<T1, T2, T3>;

    return base.Equals(rightT) && F.DSEquals(this.Item3, rightT.Item3);

    I always get complaints when I show Equals methods that throw if null is passed in, but I stand by my logic, that the presence of null for these categories of ‘structurally equal’ classes is symptom of an error and I want to be notified. Returning false doesn’t do that.

            internal static bool DSEquals<T>(T left, T right) {

    if (left == null && right == null)
    return true;

    if (left == null && right != null)
    return false;

    var len = left as IEnumerable;
    var ren = right as IEnumerable;

    if (len == null && ren == null)
    return left.Equals(right);

    if (len == null && ren != null)
    return false;

    if (len != null && ren == null)
    return false;

    return SequenceEqual(len, ren);

    DSEquals check the content of the Tuple and forward to SequenceEqual in case one slot of the Tuple contains an IEnumerable.

            internal static bool SequenceEqual(IEnumerable en1, IEnumerable en2) {

    var enumerator = en2.GetEnumerator();
    foreach (var o in en1) {

    if (!enumerator.MoveNext())
    return false;

    if (!DSEquals(o, enumerator.Current))
    return false;

    SequenceEqual checks that the number of items in the enumerator is the same and recursively calls DSEqual to check structural equality for items at the same index in the two enumerators.

    GetHashCode is trivial (and maybe trivially wrong, one of these days I’ll learn everything about GetHashCode() ).

            public override int GetHashCode() {

    return base.GetHashCode() | Item3.GetHashCode();

    The equality operators are equally simple.

            public static bool operator ==(Tuple<T1, T2, T3> t1, Tuple<T1, T2, T3> t2) {


    return t1.Equals(t2);
    public static bool operator !=(Tuple<T1, T2, T3> t1, Tuple<T1, T2, T3> t2) {

    return !(t1 == t2);

    And ToString() prints my favorite Tuple format.

            public override string ToString() {

    return string.Format(“{0},{1}”, base.ToString(), Item3.ToString());

    I’m sure you can find plenty of issues in this code. As always, it is not ‘production ready’, it is more ‘Luca having fun doing it’. In any case, there are some testcases in the solution to check the extent of my testing.

    In the next post we’ll look at Records.


    Comments (16)

    1. Jacob says:

      What about:

      internal static bool DSEquals<T>(T left, T right) {

         if (left == null && right == null)

             return true;

         if (left == null && right != null)

             return false;

         var len = left as IEnumerable;

         var ren = right as IEnumerable;

         if (len == null && ren == null)

             return left.Equals(right);

         if (len != null && ren != null)

             return SequenceEqual(len, ren);

         return false;


    2. Adam Cooper says:


      Thanks so much for sharing this, both your posts and the source code.

      I really like your idea of bringing some (more) of F#’s goodness to C#. I spent some time investigating if F# was the right tool for some of our company projects and, while I think F# is fantastic, our domain is one of storing and retrieving lots of hierarchical data, a domain which is ideal for an object-oriented language like C#. So C# is the clear choice for us (in the vast majority of cases). However, there are quite a few aspects of functional programming I’m really beginning to admire.

      I’ve never been happy with the OO paradigm of many-inputs-one-output, and I find your Tuples solution exciting. Can you comment on how your Tuples implementation differs from anonymous types in C#?

      Thanks and keep up the good work,

      Adam Cooper

    3. lucabol says:

      Thanks for the kind words.

      There are two differences:

      1. Anonymous types allow you to give a name to each property, while tuple don’t.

      2. You cannot return anonymous types from methods or passing them as parameters. So their used is limited to the body of a method.

      Overall anonymous types are a mix of tuples and records. I’ll talk about records in an upcoming post.

    4. Previous posts: Part I – Background Part II – Tuples Now that we know what Tuples are, we can start talking

    5. Welcome to the forty-third issue of Community Convergence. The last few weeks have been consumed by the

    6. Configurator says:

      I’m wondering: Doesn’t CheckNull cause an infinite loop?

      Usually when checking null, I would use ReferenceEquals(t, null) because == would call the type’s operator ==, which checks for null causing an infinite loop.

      If CheckNull accepted an object and wasn’t generic, if would use object’s ==, which works for null. But since it’s generic it should use the == that you supplied.

      Am I missing something here?

    7. I’ve also needed to build these things, and have also run into some of the same issues you seem to have here – like the surprising unhandiness of structs (and even more surprising slowness).

      Incidentally, your hash-code is no good.  You don’t want to use a symmetric combination of two hash codes, since that means that F.Tuple(true,false) is equal to F.Tuple(false, true) in terms of hashcode.  A non-symmetric combination is probably best; but the instantly obvious subtraction is definitely not a good idea, since that means that all tuples of two elements with identical members have a hash-code of 0.

      I choose to scale the hashcode with a prime number.  Trial and error on my test-set (consisting of lower-case strings which may not be representative for others) found that 137 is fine; so that’s what I use, but I haven’t researched the issue.

      Further, if your tuples will be used extensively in things like lookup tables, then hash-code calculation becomes relevant:  you might want to store the hashcode once and reuse it (which can also help speed up equality comparisons when these are performed outside of a hash table).

    8. lucabol says:

      It doesn’t go into an infinite loop because it is generic. We cannot call the overload == because at that point in the code we don’t know what it is. So we box it and ‘compare the pointer’ insted. The commented IL for the function should clarify:

      .method public hidebysig static void CheckNull<T>(!!T t) cil managed


         .maxstack 2

         .locals init (

             [0] bool CS$4$0000)

         L_0000: nop

         L_0001: ldarg.0

         L_0002: box !!T // We box it

         L_0007: ldnull

         L_0008: ceq // We compare it

         L_000a: ldc.i4.0

         L_000b: ceq

         L_000d: stloc.0

         L_000e: ldloc.0

         L_000f: brtrue.s L_0017

         L_0011: newobj instance void [mscorlib]System.ArgumentNullException::.ctor()

         L_0016: throw

         L_0017: ret


    9. lucabol says:

      Eamon: I know my hash is bad. I was too lazy to produce a better one 🙂

    10. David Nelson says:

      The problem with your "this should never be null" logic is that it prevents the user from ever writing code like this:

      Tuple<int, int> MyMethod()


        Tuple<int, int> result = null;

        // Some code which may or may not set result

        if(result == null)


           // Some other code which always sets the result


        return result;


      This is a reasonably common pattern. Your code is checking for null in the wrong place. It is not unreasonable that any Tuple reference might  sometimes be null. Tuples should only be checked for null where it is sure that they should never be null.

    11. lucabol says:

      Hi David,

      I used this code to learn functional programming. In that style variables are immutable, which makes the code you present invalid. So, in my scenario, my decision makes sense.

      In a more generic sense, you have to trade off the annoyance of not being able to write the code you present and the annoyance of not catching bugs because you return false instead of throwing. Reasonable people can come up in different places in that debate.

    12. John Mertus says:

      Why not just return a dictionary from the function.  The key of the dictionary are the names of the elements of the class, the value objects.  For example

       var Person = GetPerson(<some id>);

       int age = Person["age"]).ToInt32()

       string name = Person["FirstName"].ToString();

      (Here .ToInt32 is the damn extension C# should have for objects, why the hell doesn’t it)

      Of course  

       The big problem is the data binding becomes a var in the java sense; that it is like a late binding and fails at runtime not compile time.   But it gives a better syntax and is more readable than tuples.

    13. David Nelson says:


      "Here .ToInt32 is the damn extension C# should have for objects, why the hell doesn’t it"

      Because the vast majority of types cannot be reasonably represented by an Int32. Presumably if such a ToInt32 method existed, these types would all have to throw an exception. Why would you want to add a parsing method to every single object which will fail most of the time?

    14. The Quest for Quick-and-Easy Immutable Value Objects in C#

    15. Other posts in the series: Part I – Background Part II – Tuples Part III – Records Part IV – Type Unions