(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));
- As mentioned, I propose to make tuple types structs rather than classes, so that no allocation penalty is associated with them. They should be as lightweight as possible. Arguably, structs can end up being more costly, because assignment copies a bigger value. So if they are assigned a lot more than they are created, then structs would be a bad choice.
- Tuples are values, so are copied by value, rather than by reference. Most of the time, this should not be an issue. However, if you are passing around tuples of large structs, this might have an impact on performance. Ref locals/returns can be used to work around these performance issues, though. Additionally, because they are values, modifying a copy remotely will not change the original copy. This is a good thing, but could catch some folk out.
- 여기서 의미하는 Tuples도 ValueTuple이다.
- 원소로 ValueTuple의 원소에 참조 타입을 넣을 수 있지만 그러지 말자.
- ValueTuple은 매우 간단하게 사용할 원소만 저장하고 가볍게 사용하는 게 좋다.
- 참조 타입을 원소로 사용할 거면 class로 만들자.
- :links: GitHub_05-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로 간주된다.
-
우리가 사용하는 ValueTuple

-
과거의 레거시인 Tuple

List<Tuple> , List<Struct> 주의점
index로 접근해서 struct의 element를 직접 변경 할 수 없다.
컴파일 에러 발생한다.