Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C#에서 6-튜플(6-Tuple) 생성하는 방법 완벽 정리

C#에서 Tuple<T1, T2, T3, T4, T5, T6> 클래스는 6-튜플(six-tuple)을 나타냅니다. 튜플(tuple)은 여러 개의 요소를 하나의 단위로 묶어 순서대로 저장할 수 있는 데이터 구조로, 서로 다른 자료형의 값들을 함께 관리해야 할 때 매우 유용합니다.

6-튜플의 주요 속성

Tuple<T1, T2, T3, T4, T5, T6> 클래스는 여섯 개의 읽기 전용 속성을 제공하며, 각 속성을 통해 튜플의 구성 요소에 접근할 수 있습니다.

  • Item1 − 현재 Tuple 객체의 첫 번째 구성 요소 값을 가져옵니다.

  • Item2 − 현재 Tuple 객체의 두 번째 구성 요소 값을 가져옵니다.

  • Item3 − 현재 Tuple 객체의 세 번째 구성 요소 값을 가져옵니다.

  • Item4 − 현재 Tuple 객체의 네 번째 구성 요소 값을 가져옵니다.

  • Item5 − 현재 Tuple 객체의 다섯 번째 구성 요소 값을 가져옵니다.

  • Item6 − 현재 Tuple 객체의 여섯 번째 구성 요소 값을 가져옵니다.

참고로 6-튜플은 new 연산자를 사용하는 방법 외에도 Tuple.Create() 메서드를 통해 간결하게 생성할 수 있습니다.

예제 1: 문자열과 정수가 혼합된 6-튜플

다음은 C#에서 다양한 자료형을 포함하는 6-튜플을 생성하고 각 요소에 접근하는 예제입니다.

using System;
public class Demo {
   public static void Main(string[] args) {
      Tuple<string,int,string,int,int,string> tuple = new Tuple<string,int,string,int,int,string>("jack", 150, "pete", 300, 600, "allan");
      Console.WriteLine("Value (Item1)= " + tuple.Item1);
      Console.WriteLine("Value (Item2)= " + tuple.Item2);
      Console.WriteLine("Value (Item3)= " + tuple.Item3);
      Console.WriteLine("Value (Item4)= " + tuple.Item4);
      Console.WriteLine("Value (Item5)= " + tuple.Item5);
      Console.WriteLine("Value (Item6)= " + tuple.Item6);
      if (tuple.Item1 == "kevin") {
         Console.WriteLine("Exists: Tuple Item 1 = " +tuple.Item1);
      }
      if (tuple.Item2 == 250) {
         Console.WriteLine("Exists: Tuple Item 2 = " +tuple.Item2);
      }
      if (tuple.Item3 == "pete") {
         Console.WriteLine("Exists: Tuple Item 3 = " +tuple.Item3);
      }
      if (tuple.Item4 == 300) {
         Console.WriteLine("Exists: Tuple Item 4 = " +tuple.Item4);
      }
      if (tuple.Item5 == 400) {
         Console.WriteLine("Exists: Tuple Item 5 = " +tuple.Item5);
      }
      if (tuple.Item6 == "allan") {
         Console.WriteLine("Exists: Tuple Item 6 = " +tuple.Item6);
      }
   }
}

실행 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다. 조건문이 참인 경우에만 해당 항목의 존재 여부가 함께 표시됩니다.

Value (Item1)= jack
Value (Item2)= 150
Value (Item3)= pete
Value (Item4)= 300
Value (Item5)= 600
Value (Item6)= allan
Exists: Tuple Item 3 = pete
Exists: Tuple Item 4 = 300
Exists: Tuple Item 6 = allan

예제 2: 동일한 정수형 타입의 6-튜플

이번에는 모든 요소가 int형인 6-튜플을 생성하는 또 다른 예제를 살펴보겠습니다.

using System;
public class Demo {
   public static void Main(string[] args) {
      Tuple<int,int,int,int,int,int> tuple = new Tuple<int,int,int,int,int,int>(100, 150, 200, 300, 600, 1000);
      Console.WriteLine("Value (Item1)= " + tuple.Item1);
      Console.WriteLine("Value (Item2)= " + tuple.Item2);
      Console.WriteLine("Value (Item3)= " + tuple.Item3);
      Console.WriteLine("Value (Item4)= " + tuple.Item4);
      Console.WriteLine("Value (Item5)= " + tuple.Item5);
      Console.WriteLine("Value (Item6)= " + tuple.Item6);
      if (tuple.Item1 == 100) {
         Console.WriteLine("Exists: Tuple Item 1 = " +tuple.Item1);
      }
      if (tuple.Item2 == 250) {
         Console.WriteLine("Exists: Tuple Item 2 = " +tuple.Item2);
      }
      if (tuple.Item3 == 300) {
         Console.WriteLine("Exists: Tuple Item 3 = " +tuple.Item3);
      }
      if (tuple.Item4 == 300) {
         Console.WriteLine("Exists: Tuple Item 4 = " +tuple.Item4);
      }
      if (tuple.Item5 == 400) {
         Console.WriteLine("Exists: Tuple Item 5 = " +tuple.Item5);
      }
      if (tuple.Item6 == 500) {
         Console.WriteLine("Exists: Tuple Item 6 = " +tuple.Item6);
      }
   }
}

실행 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다. 이 예제에서는 Item1과 Item4의 조건만 참이므로 두 항목만 추가로 표시됩니다.

Value (Item1)= 100
Value (Item2)= 150
Value (Item3)= 200
Value (Item4)= 300
Value (Item5)= 600
Value (Item6)= 1000
Exists: Tuple Item 1 = 100
Exists: Tuple Item 4 = 300

정리

C#의 6-튜플은 Tuple<T1, T2, T3, T4, T5, T6> 클래스를 통해 손쉽게 생성할 수 있으며, Item1부터 Item6까지의 속성으로 각 요소에 접근합니다. 튜플은 한 번 생성하면 요소를 변경할 수 없는 불변(immutable) 구조라는 점도 기억해 두면 좋습니다.