Tuple 클래스

정의

튜플 개체를 작성하는 정적 메서드를 제공합니다.

public ref class Tuple abstract sealed
public static class Tuple
type Tuple = class
Public Class Tuple
상속
Tuple

예제

다음 예제에서는 소수가 20보다 작은 8개의 튜플(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

설명

튜플은 특정 수와 요소 시퀀스가 있는 데이터 구조입니다. 튜플의 예로는 첫 번째 요소에 사람의 이름, 두 번째 요소의 1년, 세 번째 요소에서 해당 연도에 대한 사람의 소득과 같은 식별자를 저장하는 데 사용되는 세 개의 요소(3 튜플 또는 트리플이라고 함)가 있는 데이터 구조가 있습니다. .NET Framework 1~7개의 요소가 있는 튜플을 직접 지원합니다. 또한 개체의 속성에 튜플 개체 Rest 를 중첩하여 8개 이상의 요소 튜플을 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 만들 수 있습니다.

튜플은 일반적으로 다음 네 가지 방법으로 사용됩니다.

  • 단일 데이터 집합을 나타내려면 예를 들어 튜플은 데이터베이스 레코드를 나타낼 수 있으며 해당 구성 요소는 레코드의 개별 필드를 나타낼 수 있습니다.

  • 데이터 집합에 쉽게 액세스하고 조작할 수 있도록 합니다.

  • 매개 변수(C#) 또는 ByRef 매개 변수(Visual Basic)를 사용하지 out 않고 메서드에서 여러 값을 반환합니다.

  • 단일 매개 변수를 통해 메서드에 여러 값을 전달합니다. 예를 들어 메서드에는 Thread.Start(Object) 시작 시 스레드가 실행되는 메서드에 하나의 값을 제공할 수 있는 단일 매개 변수가 있습니다. 개체를 Tuple<T1,T2,T3> 메서드 인수로 제공하는 경우 스레드의 시작 루틴에 세 개의 데이터 항목을 제공할 수 있습니다.

Tuple 클래스 자체가 튜플을 나타내지 않습니다. 대신 .NET Framework 지원하는 튜플 형식의 인스턴스를 만들기 위한 정적 메서드를 제공하는 클래스입니다. 각 튜플 구성 요소의 형식을 명시적으로 지정하지 않고도 튜플 개체를 인스턴스화하기 위해 호출할 수 있는 도우미 메서드를 제공합니다.

클래스 생성자를 호출하여 튜플 클래스의 인스턴스를 만들 수 있지만 그렇게 할 코드는 번거로울 수 있습니다. 다음 예제에서는 클래스 생성자를 사용하여 1950년부터 2000년까지 각 인구 조사에 대한 뉴욕시의 인구 데이터를 포함하는 7개의 튜플 또는 패혈을 만듭니다.

// 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 1~8개의 구성 요소(즉, 8진수에서 8개)가 있는 튜플 개체의 생성을 직접 지원합니다. 튜플에 있을 수 있는 구성 요소 수에 대한 실질적인 제한은 없지만 도우미 메서드는 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개의 요소로 구성된 새 튜플 또는 7중을 만듭니다.

Create<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6)

6개의 요소로 구성된 새 튜플 또는 6중을 만듭니다.

Create<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5)

5개의 요소로 구성된 새 튜플 또는 5중을 만듭니다.

Create<T1,T2,T3,T4>(T1, T2, T3, T4)

4개의 요소로 구성된 새 튜플 또는 4중을 만듭니다.

Create<T1,T2,T3>(T1, T2, T3)

3개의 요소로 구성된 새 튜플 또는 3중을 만듭니다.

Create<T1,T2>(T1, T2)

2개의 요소로 구성된 새 튜플 또는 쌍을 만듭니다.

Create<T1>(T1)

1개의 요소로 구성된 새 튜플 또는 singleton을 만듭니다.

적용 대상

추가 정보