ํฌ์ŠคํŠธ

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

// ํ•„๋“œ ์ด๋ฆ„์€ ๋ฌด์‹œ๋˜๊ณ  ํ• ๋‹น๋˜์ง€ ์•Š์Œ
// XXX : 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 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.