7

I was looking at examples online of Tuple but I do not see any ideal use of it.

Meaning, it seems like a place to store variables.

Is there any practical use of Tuple. What I like to do is to pass in a value to the tuple and then have it return back 3 values which are all strings.

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • 5
    What do you mean, "pass in a value to the tuple and have it return back three values"? Tuples don't *do* anything except store data. Asking what a tuple is for is like asking what a POCO is for. – Kirk Woll Jun 21 '12 at 21:24
  • 3
    Have you researched what a Tuple is before asking this question? What examples were used? Maybe consider asking a more direct question i.e. "Is this a good fit for a Tuple?" etc. I won't mark you down, but this really isn't a very good question IMO. – Nathan Jun 21 '12 at 21:50

8 Answers8

11

A Tuple is counter-part to a List.

While a List stores 0-N of the same type of item, a Tuple store 1-M of (possibly) different-typed items, where N is unbounded and M is statically fixed/defined.

Each of these items can be accessed in a strongly-typed manner by their name (or "index" as it happens to aligned).

They are thus similar to an anonymous type (actually, this is more like a "record" and not a "tuple" because the names can be arbitrarily chosen):

new { _0 = value0, _1 = value1, /* etc, as needed */ }

But the Tuple types are nominatively typed (they are backed by a bunch of different classes, just like Action or Func) and thus a specific Tuple type can be explicitly specified (e.g. in method signatures), which is something an anonymous type cannot be used for.

While I would say that the practical use of Tuples in C# is hampered by the lack of support (e.g. no decomposition, no application, etc.), they are used all the time in languages like Scala. The C# approach is generally to "create a new named type", but introduces the Tuple types as another available tool.

(A big place where Tuples are very useful is in intermediate computations -- but C# has anonymous types, which as seen with LINQ, fulfill this role quite well in most cases where the computations are done within the same method.)

7

Microsoft .NET 4.0 introduces type called Tuple which is a fixed-size collection of heterogeneously typed data. Like an array, a tuple has a fixed size that can't be changed once it has been created. Unlike an array, each element in a tuple may be a different type, and a tuple is able to guarantee strong typing for each element. This is really handy in scenario otherwise be achieved using custom types or struct.

s_nair
  • 812
  • 4
  • 12
  • Also unlike an array, the items in a `Tuple` are immutable (you cannot change their value after the `Tuple` has been instantiated) – Salmonstrikes Aug 25 '17 at 17:41
4

Tuple s a container. you can store anything in it

For 3 items, it s called Triple. 4 items quadruple and so on.

Essentially you can just stick items in to it.

Here is an example.

DarthVader
  • 52,984
  • 76
  • 209
  • 300
3

The Tuple is a typed, immutable and generic construct. It is a useful container for storing conceptually related data. A simple class with commented members and additional methods is more useful for important things. But the Tuple can be used to store miscellaneous yet related information. Tuple falls short in the field of information hiding. It excels as a useful short-term container.

HichemSeeSharp
  • 3,240
  • 2
  • 22
  • 44
2

A practical use-case: let's say you want to pass around a list of structured data between different internal components of a software.

  1. You can either declare a class which represents the structured data. In this case this class has to be dumb ideally, it'll only contain a bunch auto properties. You probably declare this in an interface as an embedded class (but then you have to prefix it with the interface name), or in the same namespace as the interface. At some point this maybe unnecessary plumbing code to define a sole class for this purpose.
  2. Or you can use a tuple. This way you don't have to define a class for all of that, you can still remain type safe. You may loose the advantage of naming the properties, which can be problematic if you have many properties, maybe even from the same type.

More concrete example: You want to set a column sorting for a TreeListView 3rd party component. You initiate the sorting from the controller object, which calls the right function (SortByColumns) on the view, which calls the function on your wrapper class around the 3rd party component, which calls the 3rd party components' proper functions.

  1. If you define a DTO (dtata transfer object) object:

    // Somewhere around an interface
    class ColumnSortItem
    {
        string Caption { get; set; }
        SortOrder Order { get; set; }
    }
    
    // Other places:
    void SortByColumns(IList<ColumnSortItem> pColumnSortItems);
    
  2. Tuples:

    void SortByColumns(IList<Tuple<string, SortOrder>> pColumnSortItems);
    

I don't say tuples are always the better choice, but notice that we just had to declare a certain order and structure of items. Note, that in this concrete example it's pretty clear what is the string part of the tuple and what is the SortOrder part.

Addition: the actual calls of the function:

  1. DTO

    controller.SortByColumns(new List<ColumnSortItem>() {
        new ColumnSortItem() { Caption = "Record", Order = SortOrder.Ascending },
        new ColumnSortItem() { Caption = "Description", Order = SortOrder.Ascending }
      });
    
  2. Tuple

    controller.SortByColumns(new List<Tuple<string, SortOrder>>() {
        new Tuple<string, SortOrder>("Record", SortOrder.Ascending),
        new Tuple<string, SortOrder>("Description", SortOrder.Ascending)
      });
    
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
  • Note, that `KeyValuePair` would be possible if the Tuple contains only two elements, but there are consequences of `KeyValuePair` being a struct: http://www.dotnetperls.com/tuple-keyvaluepair – Csaba Toth Sep 11 '13 at 18:05
1

Tuple is a lightweight class to group several items together. It's an alternative to defining a new class any time you want to group two items together.

I find it useful when I want to return multiple items from a single method, but I can't use ref or out parameters.

David Yaw
  • 27,383
  • 4
  • 60
  • 93
1

It seems like it's there for temporary data storage; very localized use. These are occasions when writing your own class is either too time consuming or really not worth it because the data's life time is so short.

Brad
  • 11,934
  • 4
  • 45
  • 73
0

The .NET Framework 4 introduce the System.Tuple class for creating tuple objects that contain structured data. It also provides generic tuple classes to support tuples that have from one to eight components . Here is example in C#

var objTupe = new System.Tuple<string, string, double,long>"Mike","Anderson",29000,9999999999);
 Response.Write("Item1 : " + objTupe.Item1);
 Response.Write("<br/>Item2 : " + objTupe.Item2);
 Response.Write("<br/>Item3 : " + objTupe.Item3);
 Response.Write("<br/>Item4 : " + objTupe.Item4);
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Jayesh Sorathia
  • 1,596
  • 15
  • 16