(int,int) 처럼 선언하는 것은 C# 7.0이후

ValueTuple과 완전히 동일하다. ValueTuple은 struct다.


//1. Normal
(int,int) a = (1,2); // => ValueTuple

//2. Container Generic
List<(int,int)> list = new List<(int,int)>();
var list = new List<(int,int)>();

//3. Long Tuple
(int,String,int,int,int) b = (1,"jiwon",2,3,4);

//4. ValueTuple의 컴파일러 동일 형식
list.Add((1, 1)); 
list.Add(new ValueTuple<int, int>(1, 1));

반드시 Naming Tuple로 사용하자.


var queue = new Queue<(int id, int workTime, int entryTime)>();
queue.Enqueue((1, 10, 0)); 

int id = member.id;
int workTime = member.workTime;
int entryTime = member.entryTime;

C# 7.0 이후,

tuple 타입의 문법 은 ValueTuple (,) 사용이 표준이며, System.Tuple은 레거시 API로 간주된다.


List<Tuple> , List<Struct> 주의점

index로 접근해서 struct의 element를 직접 변경 할 수 없다. 컴파일 에러 발생한다.