7

我在网上查看示例,Tuple但我没有看到它的任何理想用途。

意思是,它似乎是一个存储变量的地方。

有没有实际用途Tuple。我喜欢做的是将一个值传递给元组,然后让它返回 3 个都是字符串的值。

4

8 回答 8

11

元组与列表相对应。

虽然 List 存储 0-N 个相同类型的项目,但 Tuple 存储 1-M 个(可能)不同类型的项目,其中 N 是无界的,而 M 是静态固定/定义的。

这些项目中的每一个都可以通过它们的名称(或碰巧对齐时的“索引”)以强类型的方式访问。

因此它们类似于匿名类型(实际上,这更像是“记录”而不是“元组”,因为名称可以任意选择):

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

但是 Tuple 类型是指定类型的(它们由一堆不同的类支持,就像Actionor一样Func),因此可以显式指定特定的Tuple 类型(例如在方法签名中),这是匿名类型不能使用的。

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.)

于 2012-06-21T21:27:18.387 回答
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.

于 2012-06-21T21:34:04.827 回答
4

元组 sa 容器。你可以在里面存储任何东西

对于 3 个项目,它被称为 Triple。4 项目四倍等。

本质上,您可以将物品放入其中。

这是一个例子

于 2012-06-21T21:25:13.400 回答
3

Tuple 是一个类型化的、不可变的和通用的构造。它是用于存储概念相关数据的有用容器。一个带有注释成员和附加方法的简单类对于重要的事情更有用。但是元组可用于存储杂项但相关的信息。元组在信息隐藏领域存在不足。它是一个有用的短期容器。

于 2012-06-21T21:26:59.547 回答
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)
      });
    
于 2013-09-10T23:54:23.050 回答
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.

于 2012-06-21T21:27:33.427 回答
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.

于 2012-06-21T21:27:40.793 回答
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);
于 2012-06-26T08:51:46.643 回答