C# 12에 도입될 예정인 기능에 대해 살펴봅니다.
Language Feature Status
=====
This document reflects the status, and planned work in progress, for the compiler team. It is a live document
and will be updated as work progresses, features are added / removed, and as work on feature progresses.
This is not an exhaustive list of our features but rather the ones which have active development
efforts behind them.
# Working Set
| Feature | Branch | State | Developer | Reviewer | IDE Buddy | LDM Champ |
| ------- | ------ | ----- | --------- | -------- | --------- | --------- |
| [Params-collections](https://github.com/dotnet/csharplang/issues/7700) | [ParamsCollections](https://github.com/dotnet/roslyn/tree/features/ParamsCollections) | [In Progress](https://github.com/dotnet/roslyn/issues/71137) | [AlekseyTs](https://github.com/AlekseyTs) | [RikkiGibson](https://github.com/RikkiGibson), [333fred](https://github.com/333fred) | | [MadsTorgersen](https://github.com/MadsTorgersen), [AlekseyTs](https://github.com/AlekseyTs) |
| [Semi-auto-properties](https://github.com/dotnet/csharplang/issues/140) | [semi-auto-props](https://github.com/dotnet/roslyn/tree/features/semi-auto-props) | [In Progress](https://github.com/dotnet/roslyn/issues/57012) | [Youssef1313](https://github.com/Youssef1313) | [333fred](https://github.com/333fred), [RikkiGibson](https://github.com/RikkiGibson) | | [CyrusNajmabadi](https://github.com/CyrusNajmabadi) |
| [Params Span\<T> + Stackalloc any array type](https://github.com/dotnet/csharplang/issues/1757) | [params-span](https://github.com/dotnet/roslyn/tree/features/params-span) | [In Progress](https://github.com/dotnet/roslyn/issues/57049) | [cston](https://github.com/cston) | TBD | | [jaredpar](https://github.com/jaredpar) |
| [Default in deconstruction](https://github.com/dotnet/roslyn/pull/25562) | [decon-default](https://github.com/dotnet/roslyn/tree/features/decon-default) | [In Progress](https://github.com/dotnet/roslyn/issues/25559) | [jcouv](https://github.com/jcouv) | [gafter](https://github.com/gafter) | | [jcouv](https://github.com/jcouv) |
| [Roles/Extensions](https://github.com/dotnet/csharplang/issues/5497) | [roles](https://github.com/dotnet/roslyn/tree/features/roles) | [In Progress](https://github.com/dotnet/roslyn/issues/66722) | [jcouv](https://github.com/jcouv) | [AlekseyTs](https://github.com/AlekseyTs), [jjonescz](https://github.com/jjonescz) | | [MadsTorgersen](https://github.com/MadsTorgersen) |
| [Escape character](https://github.com/dotnet/csharplang/issues/7400) | N/A | [In Progress](https://github.com/dotnet/roslyn/pull/70497) | [CyrusNajmabadi](https://github.com/CyrusNajmabadi) | [jcouv](https://github.com/jcouv), [RikkiGibson](https://github.com/RikkiGibson) | | [CyrusNajmabadi](https://github.com/CyrusNajmabadi) |
| [Method group natural type improvements](https://github.com/dotnet/csharplang/blob/main/proposals/method-group-natural-type-improvements.md) | main | In Progress | [jcouv](https://github.com/jcouv) | [AlekseyTs](https://github.com/AlekseyTs), [cston](https://github.com/cston) | | [jcouv](https://github.com/jcouv) |
| [Native lock](https://github.com/dotnet/csharplang/issues/7104) | main | [In Progress](https://github.com/dotnet/roslyn/pull/71716) | [jjonescz](https://github.com/jjonescz) | [cston](https://github.com/cston), [RikkiGibson](https://github.com/RikkiGibson) | | [stephentoub](https://github.com/stephentoub) |
This file has been truncated. show original
기능이 구현되었을 때마다 내용을 살펴보도록 하겠습니다.
6개의 좋아요
기본 생성자 (구현됨, 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개의 좋아요
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개의 좋아요
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개의 좋아요
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개의 좋아요
dimohy
5월 11, 2023, 11:25오후
12
1개의 좋아요
dimohy
5월 11, 2023, 11:26오후
13
1개의 좋아요
dimohy
5월 11, 2023, 11:26오후
14
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개의 좋아요
dimohy
7월 19, 2023, 12:44오후
17
C# 12의 기능이 거의 확정되었습니다. 꽤 많은 기능이 C# 12에서 제외된다는 사실이 약간 슬프네요.
특히 반자동 속성(Semi auto properties)의 경우 꽤 많이 사용할 기능이라 이전 버전부터 기다렸는데 의외로 구현이 어려운가 봅니다.
3개의 좋아요
dimohy
7월 20, 2023, 12:02오후
18
드디어 컬렉션 리터럴이 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개의 좋아요
dimohy
7월 26, 2023, 12:02오전
19
컬렉션 표현식 (구현됨, 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개의 좋아요