[TIL] 127 [C#] νν(Tuple)
μΉ΄ν κ³ λ¦¬: Til
Tuple
νν(Tuple)
ννμ κ°λ¨ν λ°μ΄ν°λ₯Ό κ·Έλ£ΉμΌλ‘ λ¬Άμ΄ κ΄λ¦¬ λ κ°μ λ°μ΄ν°λ₯Ό μ μ₯
ννμ μμκ° μλ λ°μ΄ν°μ 컬λ μ
λ°μ΄ν°λ μλ‘ λ€λ₯Έ λ°μ΄ν° νμ
μ κ°μ§ μ μμ΅λλ€.(int, string)
μμ
νν μ¬μ© μμ
var tuple = (1, "Kim", true);
Console.WriteLine(tuple.Item1); // 1
Console.WriteLine(tuple.Item2); // Kim
Console.WriteLine(tuple.Item3); // true
(int id, string name, bool isActive) user = (1, "Kim", true);
Console.WriteLine(user.id); // 1
Console.WriteLine(user.name); // Kim
Console.WriteLine(user.isActive); // True
νν μ¬μ© μμ (int,int), 리μ€νΈ, ν
// νν μ¬μ© 1
(int, int) pair = (10, 20);
Console.WriteLine($"First: {pair.Item1}, Second: {pair.Item2}");
// μΆλ ₯ : First: 10, Second: 20
-------------------------------------------------------
// νν μ¬μ© 2 - λͺ
λͺ
λ νν
(int x, int y) namedPair = (10, 20);
Console.WriteLine($"X: {namedPair.x}, Y: {namedPair.y}");
// μΆλ ₯ : X: 10, Y: 20
-------------------------------------------------------
// νν μ¬μ© 3 - 리μ€νΈ
List<(int, int)> pairs = new List<(int, int)>
{
(1, 2),
(3, 4),
(5, 6)
};
foreach (var pair in pairs)
{
Console.WriteLine($"First: {pair.Item1}, Second: {pair.Item2}");
}
// μΆλ ₯:
// First: 1, Second: 2
// First: 3, Second: 4
// First: 5, Second: 6
// νν μ¬μ© 4 - ν
Queue<(int, int)> queue = new Queue<(int, int)>();
queue.Enqueue((30, 40));
var (curX, curY) = queue.Dequeue();
int nx = curX;
int ny = curY;
μ‘λ΄, μΌκΈ°?
ννλ κ°λ¨ν λ κ°μ λ°μ΄ν° κ·Έλ£Ήμ λ€λ£¨λ λ° λ§€μ° μ ν©ν©λλ€.
λ 볡μ‘ν λ°μ΄ν° κ΅¬μ‘°κ° νμνλ©΄ ν΄λμ€λ ꡬ쑰체λ₯Ό μ¬μ©νλ κ²λ μ’μ λ°©λ²μ
λλ€.
λκΈλ¨κΈ°κΈ°