Tuple 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供用于创建元组对象的静态方法。
public ref class Tuple abstract sealed
public static class Tuple
type Tuple = class
Public Class Tuple
- 继承
-
Tuple
示例
以下示例创建一个包含小于 20 的质数的 8 元组(八进制数)。
var primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19);
Console.WriteLine("Prime numbers less than 20: " +
"{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7}",
primes.Item1, primes.Item2, primes.Item3,
primes.Item4, primes.Item5, primes.Item6,
primes.Item7, primes.Rest.Item1);
// The example displays the following output:
// Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
open System
let primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19)
printfn $"Prime numbers less than 20: {primes.Item1}, {primes.Item2}, {primes.Item3}, {primes.Item4}, {primes.Item5}, {primes.Item6}, {primes.Item7}, and {primes.Rest.Item1}"
// Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
Dim primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19)
Console.WriteLine("Prime numbers less than 20: " +
"{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7}",
primes.Item1, primes.Item2, primes.Item3,
primes.Item4, primes.Item5, primes.Item6,
primes.Item7, primes.Rest.Item1)
' The example displays the following output:
' Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
注解
元组是具有特定数量和元素序列的数据结构。 元组的一个示例是一个数据结构,其中包含三个元素(称为三元组或三元组),用于存储标识符,例如第一个元素中的人员姓名、第二个元素中的年份,以及第三个元素中该年度的人员的收入。 .NET Framework 直接支持具有一到七个元素的元组。 此外,还可以通过在对象的属性中 Rest 嵌套元组对象来创建包含 8 个或更多元素的 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 元组。
元组通常采用四种方式:
表示单个数据集。 例如,元组可以表示数据库记录,其组件可以表示记录的各个字段。
为了提供对数据集的轻松访问和操作。
在不使用
out参数(在 C# 中)或ByRef参数(在 Visual Basic 中)的情况下从方法返回多个值。通过单个参数将多个值传递给方法。 例如, Thread.Start(Object) 该方法具有一个参数,用于向线程在启动时执行的方法提供一个值。 如果提供对象 Tuple<T1,T2,T3> 作为方法参数,则可以提供线程的启动例程,其中包含三个数据项。
该 Tuple 类本身不表示元组。 而是提供静态方法的类,用于创建 .NET Framework 支持的元组类型的实例。 它提供了帮助程序方法,你可以调用实例化元组对象,而无需显式指定每个元组组件的类型。
尽管可以通过调用元组类构造函数来创建元组类的实例,但要执行此操作的代码可能很繁琐。 下面的示例使用类构造函数创建一个 7 元组或隔隔,其中包含纽约市从 1950 年到 2000 年的每个人口普查的人口数据。
// Create a 7-tuple.
var population = new Tuple<string, int, int, int, int, int, int>(
"New York", 7891957, 7781984,
7894862, 7071639, 7322564, 8008278);
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
population.Item1, population.Item7);
// The example displays the following output:
// Population of New York in 2000: 8,008,278
// Create a 7-tuple.
let population = Tuple<string, int, int, int, int, int, int>(
"New York", 7891957, 7781984,
7894862, 7071639, 7322564, 8008278)
// Display the first and last elements.
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
// The example displays the following output:
// Population of New York in 2000: 8,008,278
' Create a 7-tuple.
Dim population As New Tuple(Of String, Integer, Integer, Integer, Integer, Integer, Integer) _
("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
' Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
population.Item1, population.Item7)
' The example displays the following output:
' Population of New York in 2000: 8,008,278
使用帮助程序方法创建同一元组对象更为简单,如以下示例所示。
// Create a 7-tuple.
var population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
population.Item1, population.Item7);
// The example displays the following output:
// Population of New York in 2000: 8,008,278
// Create a 7-tuple.
let population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
// Display the first and last elements.
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
// The example displays the following output:
// Population of New York in 2000: 8,008,278
' Create a 7-tuple.
Dim population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
' Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
population.Item1, population.Item7)
' The example displays the following output:
' Population of New York in 2000: 8,008,278
Create帮助程序方法直接支持创建包含一到八个组件的元组对象(即通过八进制数的单一实例)。 尽管元组可能具有的组件数量没有实际限制,但帮助程序方法无法创建包含 9 个或更多组件的元组。 若要创建此类元组,必须调用 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 构造函数。
注释
有关使用元组的其他信息和示例,请参阅 .NET Framework 中各个元组类型的文档。 本主题末尾的“另请参阅”部分列出了这些内容。
方法
| 名称 | 说明 |
|---|---|
| Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8) |
创建新的 8 元组或八进制。 |
| Create<T1,T2,T3,T4,T5,T6,T7>(T1, T2, T3, T4, T5, T6, T7) |
创建新的 7 元组或 9uple。 |
| Create<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6) |
创建新的 6 元组或性交集。 |
| Create<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5) |
创建新的 5 元组或 quintuple。 |
| Create<T1,T2,T3,T4>(T1, T2, T3, T4) |
创建新的 4 元组或四元组。 |
| Create<T1,T2,T3>(T1, T2, T3) |
创建新的 3 元组或三元组。 |
| Create<T1,T2>(T1, T2) |
创建新的 2 元组或对。 |
| Create<T1>(T1) |
创建新的 1 元组或单一实例。 |