C # Tuple - What is the practical use of Tuple

I watched Tuple examples online, but I do not see its perfect use.

The value seems to be the location of the variables.

Is there a practical use for Tuple . What I like to do is pass the value to the tuple and then return back 3 values ​​that are integers.

+6
source share
8 answers

Microsoft.NET 4.0 introduces a type called Tuple , which is a collection of heterogeneous, typed data of a fixed size. Like an array, a tuple has a fixed size, which cannot be changed after it is created. Unlike an array, each element in a tuple can be of a different type, and a tuple can guarantee strong typing for each element. This is really convenient in a script, otherwise it is achieved using custom types or structure.

+7
source

A Tuple is the counterpart of the list.

While List stores 0-N of the same type, Tuple stores 1-M (possibly) different types of elements, where N is unbounded and M is statically fixed / defined.

Each of these elements can be accessed in a strongly typed manner by name (or “index”, as happens for alignment).

Thus, they look like an anonymous type (in fact, it is more likely a “record” rather than a “tuple”, because names can be arbitrarily selected):

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

But Tuple types are nominatively typed (a bunch of different classes support them, like t21 or Func ), and therefore a specific Tuple type can be explicitly specified (for example, in method signatures), which cannot be used for an anonymous type.

Although I would say that the practical use of Tuples in C # is hampered by the lack of support (for example, without decomposition, without an application, etc.), they are always used in languages ​​like Scala. C #'s approach usually is to "create a new named type", but introduce Tuple types as another available tool.

(A great place where Tuples are very useful is intermediate calculations, but C # has anonymous types, which, as seen from LINQ, perform this role pretty well in most cases when the calculations are performed by the same method.)

+9
source

Tuple is a container. you can store anything in it

For 3 items, it is called Triple. 4 elements four times, etc.

Essentially, you can just insert elements into it.

Here is an example.

+3
source

A tuple is a typed, immutable, and general construct. This is a useful container for storing conceptually related data. A simple class with participant comments and additional methods is more useful for important things. But Tuple can be used to store different but related information. The cortege is not enough in the field of information hiding. It stands out as a useful short-term container.

+3
source

A practical use case: let's say you want to transfer a list of structured data between various internal software components.

  • You can declare a class representing structured data. In this case, this class should be ideally mute; it will contain only automatic beam properties. You probably declare this in the interface as an embedded class (but then you must prefix it with the name of the interface) or in the same namespace as the interface. At some point, this may be an unnecessary plumbing code to define for this single class.
  • Or you can use a tuple. Thus, you do not need to define a class for all this, you can still remain type safe. You may lose the advantage of naming properties, which can be problematic if you have many properties, perhaps even one type.

A more specific example: You want to set column sorting for a third-party TreeListView component. You initiate sorting from a controller object that calls the correct function ( SortByColumns ) in the view, which calls the function in your wrapper class around the third-party component, which calls the corresponding functions of the third-party components.

  • If you define a DTO object (data transfer object):

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

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

I'm not saying that tuples are always the best choice, but note that we just need to declare a specific order and structure of elements . Note that in this particular example, it is pretty clear what the string part of the tuple is and what part of SortOrder.

Addition: actual function calls:

  • DTO

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

     controller.SortByColumns(new List<Tuple<string, SortOrder>>() { new Tuple<string, SortOrder>("Record", SortOrder.Ascending), new Tuple<string, SortOrder>("Description", SortOrder.Ascending) }); 
+2
source

Tuple is a lightweight class for grouping multiple elements together. This is an alternative to defining a new class anytime you want to group two elements together.

I find this useful when I want to return multiple elements from the same method, but I cannot use the ref or out parameters.

+1
source

It seems to be there for temporary data storage; very localized use. These are cases when you write your own class, either too time-consuming or really not worth it, because the data life is so short.

+1
source

The .NET Framework 4 introduces the System.Tuple class to create collection objects that contain structured data. It also provides generic tuple classes to support tuples, which have one to eight components. Here is an 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); 
0
source

Source: https://habr.com/ru/post/918704/


All Articles