.NET 4 – Tuple instead of out parameters

.NET 4 introduced a tuple class which can be very useful.

Wikipedia defines a Tuple as “… a tuple is an ordered list of elements.”

MSDN defines a Tuple as “A tuple is a data structure that has a specific number and sequence of elements.”

You may be asking right now, “What am I supposed to do with that?”  I hope to give you a good idea by the time you are done reading this post.  I will provide a few examples of why, when and how to use them.  Let’s get to it.

One of my pet peeves is output parameters also known as ByRef in VB.  I know there many reasons to use them but the default seems to be where you need to return more than one value from a function.  Consider the following:

string outParameter1, outParameter2;
var v = OutParameterFunction("Test", out outParameter1, out outParameter2);
Console.WriteLine("return value: {0} :: out parameters: {1}, {2}", v, outParameter1, outParameter2);

public static int OutParameterFunction(string inParameter, out string outParameter1, out string outParameter2)
     var retValue = 0;
     outParameter1 = null;

     if (!string.IsNullOrEmpty(inParameter))
         outParameter1 = inParameter;
         outParameter2 = "Kewl";
         retValue = 1;
     else outParameter2 = "NOT Kewl";

     return retValue;

Simply put there are two values being returned with one of them being the integer value and the other being the output string parameter.

It would be easy to circumvent issue by creating a class to hold the multiple values and return the class.  What happens when you have a bunch of permutations of data, you could end up having dozens of container classes therefore increasing the cost of maintenance.

Another option would be to use an array of object.  Although it can be done, it has a huge performance impact and

The tuple option can be like this:

var t = TupleFunction("Test");
Console.WriteLine("return value: {0} :: {1}", t.Item1, t.Item2, t.Item3);

public static Tuple<int, string, string> TupleFunction(string inParameter)
var retInt = 0;
var retString1 = string.Empty;
string retString2;

     if (!string.IsNullOrEmpty(inParameter))
retString1 = inParameter;
retString2 = "Kewl";
retInt = 1;
else retString2 = "NOT Kewl";

     return Tuple.Create(retInt, retString1, retString2);

The code performs the same overall function but does not rely on output parameters.  You may look at the source as the same length and seems to hide the vales inside of a strange object.  The caller of the Tuple function does not have to create a container variable except for the return value.  Consider the situation where the call needs to be extended to return a third value, or a forth?  The tuple can be extended with more values and the caller can deal with the longer return set or not as the caller’s choice (if you like var) because the actual call is not determined at run time but at compile time.

There are some drawbacks to tuples:

  1. You can’t change the values once placed into a tuple.
  2. Unlike list types, you can’t iterate through the values.

I hope this helps.