๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90

 

 

 

ํŠœํ”Œ (Tuple)

์—ฌ๋Ÿฌ ํ•„๋“œ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์ฒด๋กœ, ์—ญ์‹œ ๊ฐ’ ํ˜•์‹์ด๋‹ค.

์ฝ๊ธฐ ์ „์šฉ์ด ์•„๋‹ˆ๋ฏ€๋กœ, ํŠœํ”Œ ๊ตฌ์„ฑ ํ•„๋“œ์˜ ๊ฐ’ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ตฌ์กฐ์ฒด์™€ ๋‹ฌ๋ฆฌ, ํŠœํ”Œ์€ ํ˜•์‹ ์ด๋ฆ„์ด ์—†๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ฆ‰์„์—์„œ ์‚ฌ์šฉํ•  ๋ณตํ•ฉ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ์„ ์–ธํ•  ๋•Œ ์ ํ•ฉํ•˜๋‹ค.

 

 

๋ช…๋ช…๋˜์ง€ ์•Š์€ ํŠœํ”Œ(Unnamed Tuple)

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•„๋“œ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ํŠœํ”Œ์„ ๋ช…๋ช…๋˜์ง€ ์•Š์€ ํŠœํ”Œ(Unnamed Tuple)์ด๋ผ๊ณ  ํ•œ๋‹ค.

var tuple = (123, 789);

 

ํŠœํ”Œ์€ ๊ด„ํ˜ธ ์‚ฌ์ด์— ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ•„๋“œ๋ฅผ ์ง€์ •ํ•จ์œผ๋กœ์จ ๋งŒ๋“ค์–ด ์ง„๋‹ค. ์œ„ ์˜ˆ์ œ์ฒ˜๋Ÿผ ์ƒ์„ฑํ•œ ๊ฒฝ์šฐ,

C# ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ž๋™์ ์œผ๋กœ 123์„ Item1์ด๋ผ๋Š” ํ•„๋“œ์— ๋‹ด๊ณ , 789๋ฅผ Item2๋ผ๋Š” ํ•„๋“œ์— ๋‹ด๋Š”๋‹ค.

var tuple = (123, 789);
Console.WriteLine($"{tuple.Item1}, {tuple.Item2}");  // 123, 789 ์ถœ๋ ฅ

 

ํŠœํ”Œ์ด System.ValueTuple ๊ตฌ์กฐ์ฒด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์ด ๋‹ด๊ธด๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

๋ช…๋ช…๋œ ํŠœํ”Œ(Named Tuple)

ํ•„๋“œ๋ช…:์˜ ๊ผด๋กœ ํ•„๋“œ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์—ฌ ์„ ์–ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

var tuple = (Name:"DaekYou", Age: 25);
Console.WriteLine($"{tuple.Name}, {tuple.Age}");  // DaekYou, 25 ์ถœ๋ ฅ

 

ํŠœํ”Œ ๋ถ„ํ•ดํ•˜๊ธฐ

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŠœํ”Œ์„ ๋ถ„ํ•ดํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

var tuple = (Name:"DaekYou", Age: 25);
var (name, age) = tuple;              // ๋ถ„ํ•ด
Console.WriteLine($"{name}, {age}");  // DaekYou, 25 ์ถœ๋ ฅ

 

ํŠน์ • ํ•„๋“œ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, _๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

var tuple = (Name:"DaekYou", Age: 25);
var (name, _) = tuple;                // Age ํ•„๋“œ๋Š” ๋ฌด์‹œ
Console.WriteLine($"{name}");         // DaekYou ์ถœ๋ ฅ

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋ฅผ ๋‹จ ๋ฒˆ์— ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ๋ถ„ํ•ดํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

var (name, age) = ("DaekYou", 25);
Console.WriteLine($"{name}, {age}");         // DaekYou, 25 ์ถœ๋ ฅ

 

 

switch ๋ฌธ ๋˜๋Š” switch ์‹์˜ ๋ถ„๊ธฐ ์กฐ๊ฑด์— ํ™œ์šฉ ํ•ด๋ณด๊ธฐ

ํŠœํ”Œ์ด ๋ถ„ํ•ด๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ๋ถ„ํ•ด์ž(Deconstructor)๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋ถ„ํ•ด์ž๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•œ ๊ฒฐ๊ณผ๋ฅผ switch ๋ฌธ์ด๋‚˜ switch ์‹์˜ ๋ถ„๊ธฐ ์กฐ๊ฑด์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๊ฒƒ์„ ์œ„์น˜ ํŒจํ„ด ๋งค์นญ(Positional Pattern Matching)์ด๋ผ๊ณ  ํ•œ๋‹ค.

์‹๋ณ„์ž๋‚˜ ๋ฐ์ดํ„ฐ ํ˜•์‹์ด ์•„๋‹Œ, ๋ถ„ํ•ด๋œ ์š”์†Œ์˜ ์œ„์น˜์— ๋”ฐ๋ผ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

var daekYou = (job:"ํ•™์ƒ", age: 17);

var discountRate = daekYou switch
             {
                 ("ํ•™์ƒ", int n) when n < 18 => 0.2,   // ํ•™์ƒ & 18์„ธ ๋ฏธ๋งŒ
                 ("ํ•™์ƒ", _)                 => 0.1,   // ํ•™์ƒ & 18์„ธ ์ด์ƒ
                 ("์ผ๋ฐ˜", int n) when n < 18 => 0.1,   // ์ผ๋ฐ˜ & 18์„ธ ๋ฏธ๋งŒ
                 ("์ผ๋ฐ˜", _)                 => 0.05,  // ์ผ๋ฐ˜ & 18์„ธ ์ด์ƒ
                 _ => 0,
             };

/*  C# 8.0 ์ด์ƒ ๋ฒ„์ „์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฌธ๋ฒ•์ธ ๊ฒƒ ๊ฐ™๋‹ค.  */

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

static double GetDiscountRate(object client)
{
    return cleint switch
             {
                 ("ํ•™์ƒ", int n) when n < 18 => 0.2,   // ํ•™์ƒ & 18์„ธ ๋ฏธ๋งŒ
                 ("ํ•™์ƒ", _)                 => 0.1,   // ํ•™์ƒ & 18์„ธ ์ด์ƒ
                 ("์ผ๋ฐ˜", int n) when n < 18 => 0.1,   // ์ผ๋ฐ˜ & 18์„ธ ๋ฏธ๋งŒ
                 ("์ผ๋ฐ˜", _)                 => 0.05,  // ์ผ๋ฐ˜ & 18์„ธ ์ด์ƒ
                 _ => 0,
             };
}


static void Main(string[] args)
{
    var alice   = (job:"ํ•™์ƒ", age: 17);
    var bob     = (job:"ํ•™์ƒ", age: 23);
    var charlie = (job:"์ผ๋ฐ˜", age: 15);
    var dave    = (job:"์ผ๋ฐ˜", age: 21);
    
    Console.WriteLine($"alice      :  {GetDiscountRate(alice)}");      // alice : 0.2
    Console.WriteLine($"bob        :  {GetDiscountRate(bob)}");        // bob : 0.1
    Console.WriteLine($"charlie    :  {GetDiscountRate(charlie)}");    // charlie : 0.1
    Console.WriteLine($"dave       :  {GetDiscountRate(dave)}");       // dave : 0.05
}

 

๋ช…๋ช…๋˜์ง€ ์•Š์€ ํŠœํ”Œ๊ณผ ๋ช…๋ช…๋œ ํŠœํ”Œ์€ ํ•„๋“œ์˜ ์ˆ˜์™€ ํ˜•์‹์ด ๊ฐ™์œผ๋ฉด ํ• ๋‹น ๊ฐ€๋Šฅํ•˜๋‹ค.

var unnamed = ("DaekYou", 25);                           // (string, int)
var named = (Name: "CoCo", Age: 1);                      // (string, int)

named = unnamed;
Console.WriteLine($"{named.Name}, {named.Age}");         // DaekYou, 25 ์ถœ๋ ฅ

named = ("Rengar", 5);

unnamed = named;
Console.WriteLine($"{unnamed.Item1}, {unnamed.Item2}");  // Rengar, 5 ์ถœ๋ ฅ

 

 

 

 

 

 

  • ์ด ๊ธ€์€ <์ด๊ฒƒ์ด C#์ด๋‹ค> ์ฑ…์„ ๋ฐ”ํƒ•์œผ๋กœ ๊ณต๋ถ€ํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•