C# Tuple
C# Tuple
π« Tuple
μ¬λ¬ κ°μ κ°λ³ κ°μ μ΄μ©νμ¬ λ¨μΌμ λ³΅ν© κ°μ λνλ΄λ λ°μ΄ν° ꡬ쑰.
κ°κ°μ κ°μ΄ μλ‘ μ°κ΄λμ΄ μμμλ μλ‘μ΄ νμ
μ μμ±νκ³ μΆμ§ μλ€λ©΄,
μΆκ°μ μΈ μΊ‘μν μμ
μμ΄ κ°λ¨ν λ³΅ν© κ΅¬μ±μ μ¬μ©ν μ μλ€.
π«§
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(int, int) t = (0, 1);
// t.ToString();
// Output: (0, 1)
// t.GetHashCode();
(double, int) t = (4.5, 3);
// t.Item1, t.Item2
(double Sum, int) t = (4.5, 3);
// t.Sum(=Item1), t.Item2
(double Sum, int Count) t = (4.5, 3);
// t.Sum(=Item1), t.Count(=Item2)
var t = (4.5, 3);
// t.Item1, t.Item2
var t = (Sum: 4.5, 3);
// t.Sum(=Item1), t.Item2
var t = (Sum: 4.5, Count: 3);
// t.Sum(=Item1), t.Count(=Item2)
(double Sum, int Count) t = (Sum: 4.5, Count: 3);
// t.Sum, t.Count
// CS8123 : νν μμ μ΄λ¦ 'Sum'μ(λ) λμ νμ '(double Sum, int Count)'μμ λ€λ₯Έ μ΄λ¦μ΄ μ§μ λμκ±°λ μ΄λ¦μ΄ μ§μ λμ§ μμκΈ° λλ¬Έμ 무μλ©λλ€.
var sum = 4.5;
var count = 3;
var t = (sum, count);
Console.WriteLine($"Sum of {t.count} elements is {t.sum}.");
// Tuple Projection Initializer
// Can't Projection : Tuple's Member Name(ToString, Rest, Item1, Item2...), Already Used
var t =
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26);
// t.Item1 ~ t.Item26
μ»΄νμΌ μκ°μ νλ μ΄λ¦μ κΈ°λ³Έ μ΄λ¦μΌλ‘ λ³κ²½
= λͺ
μμ μ§μ , μ μΆλ νλ μ΄λ¦ λ°νμ μ¬μ© λΆκ°λ₯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var xs = new[] { 4, 7, 9 };
var limits = FindMinMax(xs);
Console.WriteLine($"Limits of [{string.Join(" ", xs)}] are {limits.min} and {limits.max}");
// Output:
// Limits of [4 7 9] are 4 and 9
var ys = new[] { -9, 0, 67, 100 };
var (minimum, maximum) = FindMinMax(ys);
Console.WriteLine($"Limits of [{string.Join(" ", ys)}] are {minimum} and {maximum}");
// Output:
// Limits of [-9 0 67 100] are -9 and 100
(int min, int max) FindMinMax(int[] input)
{
if (input is null || input.Length == 0)
{
throw new ArgumentException("Cannot find minimum and maximum of a null or empty array.");
}
var min = int.MaxValue;
var max = int.MinValue;
foreach (var i in input)
{
if (i < min)
{
min = i;
}
if (i > max)
{
max = i;
}
}
return (min, max);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// ν λΉ λ° λΆν΄
// λ νν νμμ μμ μκ° λμΌν¨
// AND
// κ° νν μμΉμμ μ€λ₯Έμͺ½ νν μμμ νμμ΄ ν΄λΉνλ μΌμͺ½ νν μμμ νμκ³Ό λμΌνκ±°λ ν΄λΉ νμμΌλ‘ μμμ μΌλ‘ λ³ν κ°λ₯ν¨
(int, double) t1 = (17, 3.14);
(double First, double Second) t2 = (0.0, 1.0);
t2 = t1;
// t2.First, t2.Second
(double A, double B) t3 = (2.0, 3.0);
t3 = t2;
// t3.A, t3.B
// νλ μ΄λ¦μ 무μλκ³ ν λΉλμ§ μμ
// t3.First, t3.Second
// = λμ
μ°μ°μλ₯Ό μ΄μ©ν νν μΈμ€ν΄μ€ κ°λ³ λ³μ λΆν΄
// λͺ
μμ μ μΈ
var t = ("post office", 3.6);
(string destination, double distance) = t;
(string destination, var distance) = t;
// κ΄νΈλ° var (νμνλ λ³μλ₯Ό μμμ μ μΈ, μ»΄νμΌλ¬κ° μ μΆ)
var t = ("post office", 3.6);
var (destination, distance) = t;
// κΈ°μ‘΄ λ³μ μ¬μ©
var destination = string.Empty;
var distance = 0.0;
var t = ("post office", 3.6);
(destination, distance) = t;
// νΌν©
// μμ (무μ)
var (_, _, _, a, _, b) = ~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// νν λΉκ΅
Console.WriteLine((Display(1), Display(2)) == (Display(3), Display(4)));
int Display(int s)
{
Console.WriteLine(s);
return s;
}
// Output:
// 1
// 2
// 3
// 4
// False
// == λ° != μ°μ°μλ λ¨λ½(short-circuiting) λ°©μμΌλ‘ ννμ λΉκ΅ν©λλ€. μ¦, κ°μ§ μμ μμ μμ μΆ©μ‘±νκ±°λ ννμ λμ λλ¬νλ μ¦μ μ°μ°μ΄ μ€μ§λ©λλ€. κ·Έλ¬λ λ€μ μμ μ κ°μ΄ λΉκ΅νκΈ° μ μ βλͺ¨λ β νν μμκ° νκ°λ©λλ€.
1
2
3
4
5
6
7
8
// Deconstruct
var p = new Person("John", "Quincy", "Adams", "Boston", "MA");
// Deconstruct the person object.
var (fName, lName, city, state) = p;
var (temp, _, _) = p;
π« λ©λͺ¨
μ΄ κΈ°μ¬λ μ μκΆμμ CC BY 4.0 λΌμ΄μΌμ€λ₯Ό λ°λ¦
λλ€.