위 링크에선 C# 튜플 사용 방법 다수를 제공합니다.
1. 튜플은 Class 타입이고 인스턴스들은 변경할 수 없습니다.
2. 튜플은 int, string, double 등 서로 다른 자료형들을 내부에 함께 담을 수 있습니다.
3. Tuple.Create() 생성자도 여러 인자를 사용할 수 있습니다 (string, int, bool) 등.
4. 튜플은 클래스 타입이라 튜플 객체는 GC가 관리합니다.
5. KeyValuePair을 이용하면 성능 향상에 도움이 됩니다.
아래는 간단한 C# 튜플 정렬 예제.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<Tuple<int, string>> list = new List<Tuple<int, string>>();
list.Add(new Tuple<int, string>(1, "cat"));
list.Add(new Tuple<int, string>(100, "apple"));
list.Add(new Tuple<int, string>(2, "zebra"));
// Use Sort method with Comparison delegate.
// ... Has two parameters; return comparison of Item2 on each.
list.Sort((a, b) => a.Item2.CompareTo(b.Item2));
foreach (var element in list)
{
Console.WriteLine(element);
}
}
}
==
(100, apple)
(1, cat)
(2, zebra)
링크로 방문하여 더 많은 정보 알아보세요.
0 댓글