In .Net 4.0, and VB10 that goes with that, you can now use a new type called a Tuple. The Tuple is not very fancy and something that is overlooked sometimes but it can be very handy on occasion.

This is what wikipedia has to say about a Tuple

In mathematics and computer science a tuple is an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a positive integer. There is also one 0-tuple, an empty sequence. An n-tuple is defined inductively using the construction of an ordered pair. Tuples are usually written by listing the elements within parentheses ‘( )’ and separated by commas; for example, (2, 7, 4, 1, 7) denotes a 5-tuple. Sometimes other delimiters are used, such as brackets ‘[ ]’ or angle brackets ‘⟨ ⟩’. Braces ‘{ }’ are almost never used for tuples, as they are the standard notation for sets.

Tuples are often used to describe other mathematical objects. In algebra, for example, a ring is commonly defined as a 3-tuple (E,+,×), where E is some set, and ‘+’, and ‘×’ are functions from the Cartesian product E×E to E with specific properties. In computer science, tuples are directly implemented as product types in most functional programming languages. More commonly, they are implemented as record types, where the components are labeled instead of being identified by position alone. This approach is also used in relational algebra.

And that seems like a good reason never to use it again. But the version we have for .Net can be quite handy sometimes. The documentation on MSDN is a bit sparse, but you can still learn from it.

The Tuple can have an infinite number of Generic parameters (not that you will ever need too many.

Here is a little example.

Dim t1 as new Tuple(of String)
Dim t2 as new Tuple(of String, String)
Dim t3 as new Tuple(of String, String, String)
Dim t4 as new Tuple(of String, String, String, String)
Dim t5 as new Tuple(of String, String, String, String, String)
Dim t6 as new Tuple(of String, String, String, String, String, String)
Dim t7 as new Tuple(of String, String, String, String, String, String, String)
Dim t8 as new Tuple(of String, String, String, String, String, String, String, String)```
Which you can then use as follows.

```vbnet
t2 = new Tuple(Of String, String)("test1","test2")
Console.WriteLine(t2.Item1)
Console.WriteLine(t2.Item2)

So for every generic parameter you have specified it will create a property, so that you can access its value (get/set).

In short a Tuple lets you create a class without really naming your properties.

As always, don’t overuse and use wisely, and if you really feel the need to use an octuple think again you are probably better of using a class. You should only use them for internal things never return them in an API. Even for internal things it is not something you would use a lot, just think about the guy that has to support your soft after you are gone. Quick and dirty come to mind and sometimes that’s just what you want, so good to know they exist.

pros

  • no need to create an extra class
  • fast to write(less typing)
  • functional programming
  • mathematical programming

cons

  • not very descriptive
  • less DRY
  • needs comments to explain what it is for