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

C# Tuple 클래스 – 6-튜플(6-Tuple) 사용법 완벽 정리

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

튜플의 주요 활용 사례

  • 데이터 집합에 손쉽게 접근할 때
  • 데이터 집합을 간편하게 조작할 때
  • 하나의 데이터 묶음을 표현할 때
  • 메서드에서 여러 개의 값을 반환할 때
  • 메서드에 여러 개의 값을 전달할 때

Tuple<T1,T2,T3,T4,T5,T6>의 속성

이 클래스는 각 위치의 요소 값에 접근할 수 있는 다음과 같은 6개의 읽기 전용 속성을 제공합니다.

  • Item1 – 현재 Tuple<T1,T2,T3,T4,T5,T6> 객체의 첫 번째 구성 요소 값을 가져옵니다.

  • Item2 – 현재 Tuple<T1,T2,T3,T4,T5,T6> 객체의 두 번째 구성 요소 값을 가져옵니다.

  • Item3 – 현재 Tuple<T1,T2,T3,T4,T5,T6> 객체의 세 번째 구성 요소 값을 가져옵니다.

  • Item4 – 현재 Tuple<T1,T2,T3,T4,T5,T6> 객체의 네 번째 구성 요소 값을 가져옵니다.

  • Item5 – 현재 Tuple<T1,T2,T3,T4,T5,T6> 객체의 다섯 번째 구성 요소 값을 가져옵니다.

  • Item6 – 현재 Tuple<T1,T2,T3,T4,T5,T6> 객체의 여섯 번째 구성 요소 값을 가져옵니다.

그럼 C#에서 6-튜플을 실제로 구현하는 예제를 살펴보겠습니다.

예제 1 – 문자열과 정수가 혼합된 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

이번에는 정수 타입만으로 구성된 6-튜플을 사용하는 또 다른 예제를 살펴보겠습니다.

예제 2 – 정수형 요소만 포함한 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);
      }
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

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

이처럼 Tuple<T1,T2,T3,T4,T5,T6> 클래스를 활용하면 서로 다른 타입의 데이터 6개를 하나의 객체로 깔끔하게 관리할 수 있으며, Item1~Item6 속성을 통해 각 요소에 직관적으로 접근할 수 있습니다.