Tuples came in .net 4 to store a sequence of elements. One of their intended uses is to return multiple values from a method without the need for out parameters. For example:
static void Main(string[] args)
{
var address = GetAddressTuple();
Console.WriteLine(address.Item1);
Console.WriteLine(address.Item2);
Console.WriteLine(address.Item3);
}
static Tuple<string, string, string> GetAddressTuple()
{
return Tuple.Create("16", "Made Up Street", "XX5 5XX");
}
The problem is their just not very descriptive. When your reading code Item1, Item2 and Item3 properties don’t really tell you whats going on. In the example above they store the first line of the address, street and postcode, but you can’t tell that from just looking.
A better idea is to use the ExpandoObject. This is a dynamic object that allows you to add as many parameters to it as you like. Its better to use this to return multiple values as its more descriptive:
static void Main(string[] args)
{
var address = GetAddressDynamic();
Console.WriteLine(address.FirstLine);
Console.WriteLine(address.SecondLine);
Console.WriteLine(address.Postcode);
}
static dynamic GetAddressDynamic()
{
dynamic address = new ExpandoObject();
address.FirstLine = "16";
address.SecondLine = "Made Up Street";
address.Postcode = "XX5 5XX";
return address;
}
Big problem with this though is that because an ExpandoObject is dynamic there is no intellisense. But still, in my opnion its easier to infer whats going on.
Also, another note. Tuples are read only after they have been created. This can also be another downside.