C# Next (C# 12 후보) - slog(μ™„λ£Œ)

C# 12에 λ„μž…λ  μ˜ˆμ •μΈ κΈ°λŠ₯에 λŒ€ν•΄ μ‚΄νŽ΄λ΄…λ‹ˆλ‹€.

κΈ°λŠ₯이 κ΅¬ν˜„λ˜μ—ˆμ„ λ•Œλ§ˆλ‹€ λ‚΄μš©μ„ μ‚΄νŽ΄λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.

6개의 μ’‹μ•„μš”

λͺ¨λ“  μœ ν˜•μ— 별칭 μ‚¬μš© (κ΅¬ν˜„λ¨, 17.6 P3에 반영)

C# 12은 이제 λ‹€μŒμ˜ μ½”λ“œκ°€ ν•©λ²•μž…λ‹ˆλ‹€.

using Point = (int X, int Y);

https://github.com/dotnet/csharplang/blob/main/proposals/using-alias-types.md

3개의 μ’‹μ•„μš”

κΈ°λ³Έ μƒμ„±μž (κ΅¬ν˜„λ¨, 17.6 P2에 반영)

C# 12은 이제 λ‹€μŒμ˜ μ½”λ“œμ™€ 같이 class, structμ—μ„œ μƒμ„±μž λ§€κ°œλ³€μˆ˜λ₯Ό 지정할 수 있으며 λ‚΄λΆ€μ—μ„œ μ‚¬μš©ν•  경우 μ μ ˆν•˜κ²Œ private ν•„λ“œλ‘œ 캑쳐 λ©λ‹ˆλ‹€.

class TestClass(int x, int y);

record와 λ‹€λ₯Έ 점은 x와 yκ°€ μ™ΈλΆ€λ‘œ λ…ΈμΆœλ˜μ§€ μ•ŠλŠ”λ‹€λŠ” μ μž…λ‹ˆλ‹€.

Visual Studio의 μ΅œμ‹  미리보기 λΉŒλ“œλ‘œ csporj에 λ‹€μŒμ˜ 섀정을 ν•œ ν›„ 확인이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

<PropertyGroup>
   <LangVersion>Preview</LangVersion>
</PropertyGroup>

https://github.com/dotnet/csharplang/blob/main/proposals/primary-constructors.md

3개의 μ’‹μ•„μš”

λžŒλ‹€ κΈ°λ³Έ λ§€κ°œλ³€μˆ˜ (κ΅¬ν˜„λ¨, 17.5 P2에 반영)

C# 12μ—μ„œλŠ” 이제 λžŒλ‹€μ—μ„œ κΈ°λ³Έ λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

var addWithDefault = (int addTo = 2) => addTo + 1;
addWithDefault(); // 3
addWithDefault(5); // 6

https://github.com/dotnet/csharplang/blob/main/proposals/lambda-method-group-defaults.md

6개의 μ’‹μ•„μš”
2개의 μ’‹μ•„μš”

Visual Studio 2022 17.6 P3이 μΆœμ‹œλ˜λ©΄μ„œ 이제 미리보기둜 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

  <PropertyGroup>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>
using Point = (int X, int Y);

Point x = (10, 15);
Point y = (20, 12);

var z = new Point(x.X + y.X, x.Y + y.Y);

Console.WriteLine(z);
2개의 μ’‹μ•„μš”
2개의 μ’‹μ•„μš”
2개의 μ’‹μ•„μš”

μΈμŠ€ν„΄μŠ€ 이름에 nameof μ ‘κ·Ό (κ΅¬ν˜„λ¨, 17.7 P1에 반영)

using System;
public struct C {
    public string P;
    public static string M1() => nameof(P); // Legal
    public static string M2() => nameof(P.Length); // error CS0120: An object reference is required for the non-static field, method, or property 'C.P'
}

μœ„μ˜ μ½”λ“œμ™€ 같이 정적 μ»¨ν…μŠ€νŠΈμ—μ„œ μΈμŠ€ν„΄μŠ€ 멀버에 μ—‘μ„ΈμŠ€ ν•  경우 컴파일 였λ₯˜κ°€ λ°œμƒν•˜λŠ” κ²½μš°κ°€ μžˆμŠ΅λ‹ˆλ‹€.

17.7 P1 이후 λΆ€ν„°λŠ” 이 컴파일 였λ₯˜λŠ” λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

3개의 μ’‹μ•„μš”
3개의 μ’‹μ•„μš”
2개의 μ’‹μ•„μš”

https://www.sysnet.pe.kr/2/0/13338

1개의 μ’‹μ•„μš”

https://www.sysnet.pe.kr/2/0/13339

1개의 μ’‹μ•„μš”

https://www.sysnet.pe.kr/2/0/13341

2개의 μ’‹μ•„μš”

VS 17.7p1이 릴리슀 λ˜λ©΄μ„œ κ°œμ„ λœ κΈ°λŠ₯을 ν…ŒμŠ€νŠΈν•  수 있게 λ˜μ—ˆμŠ΅λ‹ˆλ‹€. 이제 μΈμŠ€ν„΄μŠ€ 멀버도 nameof λŒ€μƒμœΌλ‘œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Console.WriteLine(C.M1());
Console.WriteLine(C.M2());


public struct C
{
    public string P;
    public static string M1() => nameof(P); // Legal
    public static string M2() => nameof(P.Length); // error CS0120: An object reference is required for the non-static field, method, or property 'C.P'
}

| 좜λ ₯

P
Length
3개의 μ’‹μ•„μš”
2개의 μ’‹μ•„μš”

C# 12의 κΈ°λŠ₯이 거의 ν™•μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€. κ½€ λ§Žμ€ κΈ°λŠ₯이 C# 12μ—μ„œ μ œμ™Έλœλ‹€λŠ” 사싀이 μ•½κ°„ μŠ¬ν”„λ„€μš”.
특히 λ°˜μžλ™ 속성(Semi auto properties)의 경우 κ½€ 많이 μ‚¬μš©ν•  κΈ°λŠ₯이라 이전 버전뢀터 κΈ°λ‹€λ ΈλŠ”λ° μ˜μ™Έλ‘œ κ΅¬ν˜„μ΄ μ–΄λ €μš΄κ°€ λ΄…λ‹ˆλ‹€.


3개의 μ’‹μ•„μš”

λ“œλ””μ–΄ μ»¬λ ‰μ…˜ λ¦¬ν„°λŸ΄μ΄ C#에 λ„μž…λ˜λŠ”κ΅°μš”. ν•œλ§ˆλ””λ‘œ 이제 λͺ©λ‘μ„ [1, 2, 3] ν˜•νƒœλ‘œ μ“Έ 수 있게 λ©λ‹ˆλ‹€.
사전 ν˜•μ‹λ„ [key: value, …]둜 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
λ‹¨μˆœνžˆ μ€‘κ΄„ν˜Έμ—μ„œ λŒ€κ΄„ν˜Έλ‘œ λ³€κ²½λœκ²ƒμ΄ μ•„λ‹ˆλΌ μ΄μ „μ—λŠ” λͺ©λ‘μ„ μ‚¬μš©ν•˜λ €λ©΄ new xx[] 둜 ν•΄μ„œ 무쑰건 νž™μ„ μ‚¬μš©ν•  수 밖에 μ—†κ±°λ‚˜ stackalloc으둜 λͺ…μ‹œμ  μŠ€νƒ 할당을 ν•΄μ•Ό ν–ˆλ‹€λ©΄ Span<T> μœ ν˜•μœΌλ‘œ 받을 경우 μž‘μ€ λ‹¨μœ„μ˜ μ»¬λ ‰μ…˜μ€ μ•”λ¬΅μ μœΌλ‘œ stackalloc을 μ‚¬μš©ν•˜κ²Œ λ©λ‹ˆλ‹€.
https://github.com/dotnet/csharplang/blob/main/proposals/collection-expressions.md#span-types

2개의 μ’‹μ•„μš”

μ»¬λ ‰μ…˜ ν‘œν˜„μ‹ (κ΅¬ν˜„λ¨, 17.7 P5에 반영)

λ“œλ””μ–΄ μ»¬λ ‰μ…˜ ν‘œν˜„μ‹μ„ Visual Studio 2022 17.7 P5μ—μ„œ ν…ŒμŠ€νŠΈ ν•΄ λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

<PropertyGroup>
   <LangVersion>Preview</LangVersion>
</PropertyGroup>

μ΄μ „μ—λŠ” μ΄λ ‡κ²Œ 써야 ν–ˆλ‹€λ©΄

var oList = new int[] { 1, 2, 3, 4, 5 };

μ΄μ œλŠ” λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

int[] nList = [1, 2, 3, 4, 5];

μŠ€νƒμ— 배열을 μƒμ„±ν•˜λŠ” 것도 λ‹€μŒμ²˜λŸΌ μ‚¬μš©ν•  수 있게 λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

Span<int> sList = [1, 2, 3, 4, 5];

νŒ¨ν„΄μΌμΉ˜μ—λ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

var result = sList is [1, .., 5];
8개의 μ’‹μ•„μš”

μ œλ„€λ¦­λ³„μΉ­μ€ μ•„μ§μΈκ°€λ³΄κ΅°μš”

2개의 μ’‹μ•„μš”