Tuples in C# provide developers with a quick way of creating data structures without the need to define a separate class, but they should be used with caution. I’ll demonstrate when you should–and shouldn’t–use tuples. Spoiler alert: I do not recommend the code in the adjacent image!
First, let’s look at how you create a tuple. Without tuples, if you needed a class with a few properties, you would create a separate POCO class, like this:
And to use this class, you would either pass values into a constructor, or use Object Initializer syntax:
While there is absolutely nothing wrong with the code above, an alternative is to use a Tuple instead of defining a class for the sole purpose of returning data. You can either use the Tuple constructor to specify the type of each element or use the Tuple.Create helper method to create the Tuple without specifying the types.
In the example above, the elements in each object are equivalent, but you’ll notice that the element names are simply “Item1, Item2, Item3″ which is obviously not very descriptive. Because of this, let’s talk about when you should and shouldn’t use Tuples.
When to use Tuples
As a general rule, you can use a Tuple when you need a privately-accessible object or instance variable where you can document what the Tuple contains, such as
- Short-lived access to an object when an anonymous type is not an option.
- Return multiple values from a private method instead of using several out parameters.
- Passing multiple parameters into a method that takes a single object parameter, such as Thread.Start() or ThreadPool.QueueUserWorkItem().
When NOT to use Tuples
Tuples are easy to use, but they should be used with caution. A few scenarios to avoid:
- Because of the non-descriptive property names (Item1, Item2, Item3), only use Tuples within a method or between private methods.
- Do not use Tuples for objects with more than a handful of elements. The Create() helper method allows up 7 elements and you can nest tuples within tuples (as demonstrated in the image above), but this is never a good idea. Create a POCO class if you need more than a few elements.
- Similar to #1, never expose a tuple through an API or when serializing an object to XML or JSON. Exposing Item1 and Item2 as property names on the client systems will only cause confusion.
Have you seen other good (or bad) examples of Tuples? Share them in the comments below!
If you are unfamiliar with the title of this blog, see: turtles all the way down.
Latest posts by Josh Eastburn (see all)
- Kendo Grid Hierarchy with Shared Column Headers - October 17, 2014
- Entity Framework and Enum Flags - September 30, 2014
- Chrome Developer Tools: FalafelCON Notes and Resources - September 22, 2014
- Server Paging, Sorting, and Filtering with Kendo DataSourceRequest - August 15, 2014
- Nested Templates with Kendo UI - August 7, 2014