[C# 6.0] ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ (1) - ํ‘œํ˜„์‹ ๋ ˆ๋ฒจ

2022. 12. 17. 10:56ยท๐Ÿ“ Language/โœ C#

C# 6.0์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ

 

C# 6.0

 

  • ํ‘œํ˜„์‹ ๋ ˆ๋ฒจ
    • ๋„ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž(Null-conditional operator)
    • ๋ฌธ์ž์—ด ๋ณด๊ฐ„(String Interpolation)
    • Dictionary Initializer
    • nameof ์—ฐ์‚ฐ์ž
  • ๋ฌธ์žฅ ๋ ˆ๋ฒจ
    • using static ๋ฌธ
    • catch/finally ๋ธ”๋ก์—์„œ await ์‚ฌ์šฉ
    • Exception Filter ์ง€์›
  • ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ ˆ๋ฒจ
    • ์ž๋™ ์†์„ฑ ์ดˆ๊ธฐ์ž(Auto Property Initializer)
    • ์ฝ๊ธฐ ์ „์šฉ ์ž๋™ ์†์„ฑ(Getter only)
    • Expression-bodied member ์‚ฌ์šฉ

 

https://www.csharpstudy.com/CS6/CSharp-6-new-features.aspx

 

C# 6.0 ์ƒˆ๊ธฐ๋Šฅ - C# ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฐ์šฐ๊ธฐ (Learn C# Programming)

๋ณธ ์›น์‚ฌ์ดํŠธ๋Š” ๊ด‘๊ณ ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด‘๊ณ  ํด๋ฆญ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ˆ˜์ต๊ธˆ์€ ๋ชจ๋‘ ์›น์‚ฌ์ดํŠธ ์„œ๋ฒ„์˜ ์œ ์ง€ ๋ฐ ๊ด€๋ฆฌ, ๊ทธ๋ฆฌ๊ณ  ๊ธฐ์ˆ  ์ฝ˜ํ…์ธ  ํ–ฅ์ƒ์„ ์œ„ํ•ด ์“ฐ์—ฌ์ง‘๋‹ˆ๋‹ค.

www.csharpstudy.com

 

1. ํ‘œํ˜„์‹ ๋ ˆ๋ฒจ

1) Null ์กฐ๊ฑด ์—ฐ์‚ฐ์ž(Null conditional operator)

๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋‚˜ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๊ฐ์ฒด๊ฐ€ Null์ธ์ง€ ํ•ญ์ƒ ์ฒดํฌํ•ด ์ค˜์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถˆํŽธ์„ ๋œ์–ด ์ฃผ๊ธฐ ์œ„ํ•ด ? ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ? ์—ฐ์‚ฐ์ž ์•ž์— ์žˆ๋Š” ๊ฐ์ฒด๊ฐ€ Null์ธ์ง€ ์ฒดํฌํ•ด์„œ Null์ด๋ฉด ๊ทธ๋ƒฅ Null์„ ๋ฆฌํ„ดํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ์˜ ์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

// rows๊ฐ€ NULL์ด๋ฉด, cnt ๋„ NULL
// rows๊ฐ€ NULL์ด ์•„๋‹ˆ๋ฉด, cnt๋Š” ์‹ค์ œ rows ๊ฐฏ์ˆ˜
int? cnt = rows?.Count;

// customers ์ปฌ๋ ‰์…˜์ด NULL์ด๋ฉด, c๋Š” NULL
// customers ์ปฌ๋ ‰์…˜์ด NULL์ด ์•„๋‹ˆ๋ฉด, c๋Š” ์ฒซ๋ฒˆ์งธ ๋ฐฐ์—ด์š”์†Œ
Customer c = customers?[0]; 

// customers๊ฐ€ ๋„์ธ์ง€ ์ฒดํฌํ•˜๊ณ  ๋‹ค์‹œ customers[0]๊ฐ€ ๋„์ธ์ง€ ์ฒดํฌ
int? age = customers?[0]?.Age;

 

? ์—ฐ์‚ฐ์ž์˜ ๋ฆฌํ„ด ๋ณ€์ˆ˜๋Š” ํ•ญ์ƒ null์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” Nullabel Type์ด์–ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ๋ฆฌํ„ด ๋ณ€์ˆ˜๊ฐ€ Null์„ ๊ฐ€์งˆ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, ?? ์—ฐ์‚ฐ์ž(Null coalescing operator)๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. Null์ธ ๊ฒฝ์šฐ ?? ๋’ค์˜ ๋””ํดํŠธ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๊ฒŒ ๋œ๋‹ค.

 

// rows๊ฐ€ NULL์ด๋ฉด, cnt๋Š” 0
// rows๊ฐ€ NULL์ด ์•„๋‹ˆ๋ฉด, cnt๋Š” ์‹ค์ œ rows ๊ฐฏ์ˆ˜
int cnt = rows?.Count ?? 0;

 

2) ๋ฌธ์ž์—ด ๋ณด๊ฐ„(String Interpolation)

C#์—์„œ ๋ฌธ์ž์—ด์„ ํฌ๋งทํŒ… ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” string.Format() ์•ˆ์˜ ์„œ์‹ ๋ฌธ์ž์—ด์— {0}, {1}, {2} ๋“ฑ๊ณผ ๊ฐ™์€ ์ธ์ˆ˜๋ฅผ ๋„ฃ์–ด ํ•ด๋‹น ์œ„์น˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์ด ๋“ค์–ด๊ฐ€๊ฒŒ ํ•˜์˜€๋‹ค. C# 6.0์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋ถˆํŽธ์„ ๋œ์–ด ์ฃผ๊ธฐ ์œ„ํ•ด {} ์•ˆ์— ์ง์ ‘ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ๊ฒŒ ๋œ๋‹ค.

 

Rectangle r = new Rectangle();
r.Height = 10;
r.Width = 32;

// Format string ์•ž์— $ ๋ฅผ ๋ถ™์ด๋ฉฐ {} ์•ˆ์— ์†์„ฑ ํ˜น์€ ๊ณ„์‚ฐ์‹ ๋“ฑ์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
string s = $"{r.Height} x {r.Width} = {(r.Height * r.Width)}";
Console.WriteLine(s);

 

3) Dictionary Initializer

๊ธฐ์กด C#์—์„œ Dictionary๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์Šคํƒ€์ผ๊ณผ ์ดˆ๊ธฐํ™” ํ›„ ์‚ฌ์šฉํ•˜๋Š” ์Šคํƒ€์ผ ๊ฐ„์— ์•ฝ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ์žˆ์—ˆ๋‹ค. C# 6.0์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์Šคํƒ€์ผ๋“ค์„ ํ†ต์ผ์‹œ์ผœ ์ดˆ๊ธฐํ™” ๊ณผ์ •์—์„œ๋„ Indexer ์Šคํƒ€์ผ์˜ ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค.

  • ๋ณด๋‹ค ์ง๊ด€์ ์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๋„์›€์ด ๋œ๋‹ค.
  • ํ•ด์‹œ ํ…Œ์ด๋ธ”, Dictionary ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ธ๋ฑ์„œ๋ฅผ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

 

// ์ด์ „์˜ C# ํ‘œํ˜„
var scores = new Dictionary<string, int>()
{
    { "kim", 100 },
    { "lee",  90 }
};
int sc = scores["lee"];

// C# 6.0 ํ‘œํ˜„
var scores = new Dictionary<string, int>()
{
    ["kim"] = 100,
    ["lee"] = 90
};
int sc = scores["lee"];

 

4) nameof ์—ฐ์‚ฐ์ž

nameof ์—ฐ์‚ฐ์ž๋Š” Type์ด๋‚˜ ๋ฉ”์„œ๋“œ, ์†์„ฑ ๋“ฑ์˜ ์ด๋ฆ„์„ ๋ฆฌํ„ดํ•œ๋‹ค.

  • ํ•˜๋“œ์ฝ”๋”ฉ์— ์˜ํ•œ ํƒ€์ดํ•‘ ์˜ค๋ฅ˜ ๋ฐฉ์ง€๋‚˜ ํ˜น์€ ์ฐจํ›„ ๋ฆฌํŒฉํ„ฐ๋ง์—์„œ ์œ ์—ฐํ•œ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

 

// ํŒŒ๋งˆ๋ฏธํ„ฐ๋ช…
throw new ArgumentException("Invalid argument", nameof(id));

// ์†์„ฑ๋ช…
Console.WriteLine("{0}: {1}", nameof(objPerson.Height), objPerson.Height);

// ๋ฉ”์„œ๋“œ๋ช…
void Run() {
   Log(nameof(Run) + " : Started");
}
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Language/โœ C#' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C# 7.0] ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ (1) - ํŒจํ„ด ๋งค์นญ
  • [C# 6.0] ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ (2) - ๋ฌธ์žฅ ๋ ˆ๋ฒจ, ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ ˆ๋ฒจ
  • [C#] Partial ํƒ€์ž…, Partial ๋ฉ”์„œ๋“œ
  • [C#] ์ธ๋ฑ์„œ(Indexer)
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C# 6.0] ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ (1) - ํ‘œํ˜„์‹ ๋ ˆ๋ฒจ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”