我在网上查看示例,Tuple
但我没有看到它的任何理想用途。
意思是,它似乎是一个存储变量的地方。
有没有实际用途Tuple
。我喜欢做的是将一个值传递给元组,然后让它返回 3 个都是字符串的值。
我在网上查看示例,Tuple
但我没有看到它的任何理想用途。
意思是,它似乎是一个存储变量的地方。
有没有实际用途Tuple
。我喜欢做的是将一个值传递给元组,然后让它返回 3 个都是字符串的值。
元组与列表相对应。
虽然 List 存储 0-N 个相同类型的项目,但 Tuple 存储 1-M 个(可能)不同类型的项目,其中 N 是无界的,而 M 是静态固定/定义的。
这些项目中的每一个都可以通过它们的名称(或碰巧对齐时的“索引”)以强类型的方式访问。
因此它们类似于匿名类型(实际上,这更像是“记录”而不是“元组”,因为名称可以任意选择):
new { _0 = value0, _1 = value1, /* etc, as needed */ }
但是 Tuple 类型是指定类型的(它们由一堆不同的类支持,就像Action
or一样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.)
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.
Tuple 是一个类型化的、不可变的和通用的构造。它是用于存储概念相关数据的有用容器。一个带有注释成员和附加方法的简单类对于重要的事情更有用。但是元组可用于存储杂项但相关的信息。元组在信息隐藏领域存在不足。它是一个有用的短期容器。
A practical use-case: let's say you want to pass around a list of structured data between different internal components of a software.
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.
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);
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:
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)
});
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.
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.
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);