C# 什么以及何时使用元组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13640322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What and When to use Tuple?
提问by Donny
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?
请有人解释一下元组是什么以及如何在真实世界场景中使用它。我想知道这如何丰富我的编码体验?
采纳答案by Adil
This msdn articleexplains it very well with examples, "A tuple is a data structure that has a specific number and sequence of elements".
这篇msdn 文章通过示例很好地解释了它,“元组是一种具有特定数量和元素序列的数据结构”。
Tuples are commonly used in four ways:
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.
To provide easy access to, and manipulation of, a data set.
To return multiple values from a method without using out parameters (in C#) or
ByRefparameters (in Visual Basic).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 aTuple<T1, T2, T3>object as the method argument, you can supply the thread's startup routine with three items of data.
元组通常以四种方式使用:
表示一组数据。例如,元组可以表示数据库记录,其组件可以表示记录的各个字段。
提供对数据集的轻松访问和操作。
在不使用输出参数(在 C# 中)或
ByRef参数(在 Visual Basic 中)的情况下从方法返回多个值。通过单个参数将多个值传递给一个方法。例如,该
Thread.Start(Object)方法有一个参数,可让您为线程在启动时执行的方法提供一个值。如果提供一个Tuple<T1, T2, T3>对象作为方法参数,则可以为线程的启动例程提供三项数据。
回答by Mark Byers
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..
通常虽然您应该创建一个类,因为这允许您为每个属性提供有用的名称。代码广泛使用的元组将很快变得不可读,因为该特性被称为Item1,Item2,Item3,等。
回答by linquize
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.
属性名称为 Item1、Item2、Item3 ...,在某些情况下或没有文档时可能没有意义。
Tuple classes have strongly typed generic parameters. Still users of the Tuple classes may infer from the type of generic parameters.
元组类具有强类型的泛型参数。元组类的仍然用户可以从泛型参数的类型推断。
回答by Kaido
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.
元组和类之间的区别在于元组没有属性名称。这几乎从来都不是一件好事,我只会在参数相当无意义时使用元组,例如在抽象数学公式中。超过 5、6、7 维的抽象微积分可能需要一个元组作为坐标。
回答by Azhar Kiani
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.
注意:属性 Item1、Item2 和其他属性没有设置器。您不能分配它们。元组在内存中创建后是不可变的。

