What and When to use Tuple? [duplicate]
Asked Answered
K

5

131

May someone please explain what a Tuple is and how to use it in a Real World Scenario. I would like to find out how this can enrich my coding experience?

Kolva answered 30/11, 2012 at 6:47 Comment(0)
P
131

This msdn article explains it very well with examples, "A tuple is a data structure that has a specific number and sequence of elements".

Tuples are commonly used in four ways:

  1. To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.

  2. To provide easy access to, and manipulation of, a data set.

  3. To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).

  4. To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple<T1, T2, T3> object as the method argument, you can supply the thread’s startup routine with three items of data.

Pamela answered 30/11, 2012 at 6:51 Comment(0)
M
80

A tuple allows you to combine multiple values of possibly different types into a single object without having to create a custom class. This can be useful if you want to write a method that for example returns three related values but you don't want to create a new class.

Usually though you should create a class as this allows you to give useful names to each property. Code that extensively uses tuples will quickly become unreadable because the properties are called Item1, Item2, Item3, etc..

Mezzosoprano answered 30/11, 2012 at 6:52 Comment(2)
I use it maximally for 2 values and each of different type i.e. string, int. Otherwise it is becoming the worst code you can write.Smilax
Keep the usage limited, perhaps preferring to only use it privately within a class/module. Also, Tuple usage containing complex types ("Customer", "Employee", etc.) can be a bit more straightforward than primitives. Nothing is worse than trying to work with a lot of Tuple<int, int, int, string, string>s.Tini
S
42

The difference between a tuple and a class is that a tuple has no property names. This is almost never a good thing, and I would only use a tuple when the arguments are fairly meaningless like in an abstract math formula Eg. abstract calculus over 5,6,7 dimensions might take a tuple for the coordinates.

Stripling answered 30/11, 2012 at 9:7 Comment(5)
In this case (where probably the types of values are the same) using a simple array is not enough? There is some advantage to use a Tuple?Valdis
The types could just as easily be different, but I guess the IComparable implementation could be an advantage in the homogenous case sometimes. As you say, I'd be inclined to use an array for simpler scenarios.Stripling
Very nice answer!Brehm
I disagree that it's almost never a good thing. There is some non-negligible overhead to having property names in your data representation. Tuples are better for your CPU.Gabbey
No longer true, because Tuple properties can be named nowCandlenut
P
28

This is the most important thing to know about the Tuple type. Tuple is a class, not a struct. It thus will be allocated upon the managed heap. Each class instance that is allocated adds to the burden of garbage collection.

Note: The properties Item1, Item2, and further do not have setters. You cannot assign them. The Tuple is immutable once created in memory.

Planography answered 3/4, 2014 at 17:22 Comment(2)
"Burden" - have you ever observed a garbage collection issue?Earthling
Absolutely! Think about network communications processing code. It's allocating and deallocating tons of memory so every couple minutes the GC is running and may take seconds to complete. This is unacceptable if something downstream is relying on that data in real-time. Another example is a resource constrained device, such as mobile device, running a real-time app, such as a driving directions app.Perithecium
R
22

Tuple classes allow developers to be 'quick and lazy' by not defining a specific class for a specific use.

The property names are Item1, Item2, Item3 ..., which may not be meaningful in some cases or without documentation.

Tuple classes have strongly typed generic parameters. Still users of the Tuple classes may infer from the type of generic parameters.

Restoration answered 30/11, 2012 at 8:11 Comment(1)
So maybe C# 7 should have made it easier to create classes / objects on the fly instead of this tuple thing?Ardehs

© 2022 - 2024 — McMap. All rights reserved.