Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 Tuple의 나머지 요소를 얻는 방법은 무엇입니까?

<시간/>

Tuple의 나머지 요소를 가져오기 위해 Rest 속성이 사용됩니다. 코드는 다음과 같습니다 -

예시

using System;
public class Demo {
   public static void Main(String[] args){
      var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, 2000);
      var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, 2000);
      Console.WriteLine("Is Tuple1 equal to Tuple2? = "+tuple1.Equals(tuple2));
      Console.WriteLine("HashCode of Tuple1 = "+tuple1.GetHashCode());
      Console.WriteLine("HashCode of Tuple2 = "+tuple2.GetHashCode());
      Console.WriteLine("Tuple1 Item 1st = "+tuple1.Item1);
      Console.WriteLine("Tuple2 Item 1st = "+tuple2.Item1);
      Console.WriteLine("Tuple1 Item 2nd = "+tuple1.Item2);
      Console.WriteLine("Tuple2 Item 2nd = "+tuple2.Item2);
      Console.WriteLine("Tuple1 Item 4th = "+tuple1.Item4);
      Console.WriteLine("Tuple2 Item 4th = "+tuple2.Item4);
      Console.WriteLine("Tuple1 Item 5th = "+tuple1.Item5);
      Console.WriteLine("Tuple2 Item 5th = "+tuple2.Item5);
      Console.WriteLine("Tuple1 Item 6th = "+tuple1.Item6);
      Console.WriteLine("Tuple2 Item 6th = "+tuple2.Item6);
      Console.WriteLine("Tuple1 Item 7th = "+tuple1.Item7);
      Console.WriteLine("Tuple2 Item 7th = "+tuple2.Item7);
      Console.WriteLine("Tuple1 rest value = "+tuple1.Rest);
      Console.WriteLine("Tuple2 rest value = "+tuple2.Rest);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Is Tuple1 equal to Tuple2? = True HashCode of Tuple1 = 3247155
HashCode of Tuple2 = 3247155
Tuple1 Item 1st = 75
Tuple2 Item 1st = 75
Tuple1 Item 2nd = 200
Tuple2 Item 2nd = 200
Tuple1 Item 4th = 700
Tuple2 Item 4th = 700
Tuple1 Item 5th = 100
Tuple2 Item 5th = 100
Tuple1 Item 6th = 1200
Tuple2 Item 6th = 1200
Tuple1 Item 7th = 1500
Tuple2 Item 7th = 1500
Tuple1 rest value = (2000)
Tuple2 rest value = (2000)

예시

이제 다른 예를 살펴보겠습니다 -

using System;
public class Demo {
   public static void Main(String[] args){
      var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, Tuple.Create("AB", 2000, "CD"));
      var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, Tuple.Create(2500, 3500, 4000, "XY"));
      Console.WriteLine("Is Tuple1 equal to Tuple2? = "+tuple1.Equals(tuple2));
      Console.WriteLine("HashCode of Tuple1 = "+tuple1.GetHashCode());
      Console.WriteLine("HashCode of Tuple2 = "+tuple2.GetHashCode());
      Console.WriteLine("Tuple1 Item 1st = "+tuple1.Item1);
      Console.WriteLine("Tuple2 Item 1st = "+tuple2.Item1);
      Console.WriteLine("Tuple1 Item 2nd = "+tuple1.Item2);
      Console.WriteLine("Tuple2 Item 2nd = "+tuple2.Item2);
      Console.WriteLine("Tuple1 Item 4th = "+tuple1.Item4);
      Console.WriteLine("Tuple2 Item 4th = "+tuple2.Item4);
      Console.WriteLine("Tuple1 Item 5th = "+tuple1.Item5);
      Console.WriteLine("Tuple2 Item 5th = "+tuple2.Item5);
      Console.WriteLine("Tuple1 Item 6th = "+tuple1.Item6);
      Console.WriteLine("Tuple2 Item 6th = "+tuple2.Item6);
      Console.WriteLine("Tuple1 Item 7th = "+tuple1.Item7);
      Console.WriteLine("Tuple2 Item 7th = "+tuple2.Item7);
      Console.WriteLine("Tuple1 rest value = "+tuple1.Rest);
      Console.WriteLine("Tuple2 rest value = "+tuple2.Rest);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Is Tuple1 equal to Tuple2? = False
HashCode of Tuple1 = -1121878415
HashCode of Tuple2 = -835095725
Tuple1 Item 1st = 75
Tuple2 Item 1st = 75
Tuple1 Item 2nd = 200
Tuple2 Item 2nd = 200
Tuple1 Item 4th = 700
Tuple2 Item 4th = 700
Tuple1 Item 5th = 100
Tuple2 Item 5th = 100
Tuple1 Item 6th = 1200
Tuple2 Item 6th = 1200
Tuple1 Item 7th = 1500
Tuple2 Item 7th = 1500
Tuple1 rest value = ((AB, 2000, CD)) Tuple2 rest value = ((2500, 3500, 4000, XY))