Microsoft MVP성태의 닷넷 이야기
.NET Framework: 2037. C# 11 - 목록 패턴(List patterns) [링크 복사], [링크+제목 복사]
조회: 8037
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 18개 있습니다.)
.NET Framework: 1110. C# 11 - 인터페이스 내에 정적 추상 메서드 정의 가능 (DIM for Static Members)
; https://www.sysnet.pe.kr/2/0/12814

.NET Framework: 1118. C# 11 - 제네릭 타입의 특성 적용
; https://www.sysnet.pe.kr/2/0/12839

.NET Framework: 1182. C# 11  - ref struct에 ref 필드를 허용
; https://www.sysnet.pe.kr/2/0/13015

.NET Framework: 2025. C# 11  - 원시 문자열 리터럴(raw string literals)
; https://www.sysnet.pe.kr/2/0/13085

.NET Framework: 2026. C# 11 - 문자열 보간 개선 2가지
; https://www.sysnet.pe.kr/2/0/13086

.NET Framework: 2030. C# 11 - UTF-8 문자열 리터럴
; https://www.sysnet.pe.kr/2/0/13096

.NET Framework: 2031. C# 11 - 사용자 정의 checked 연산자
; https://www.sysnet.pe.kr/2/0/13099

.NET Framework: 2032. C# 11 - shift 연산자 재정의에 대한 제약 완화 (Relaxing Shift Operator)
; https://www.sysnet.pe.kr/2/0/13100

.NET Framework: 2035. C# 11 - 새로운 연산자 ">>>" (Unsigned Right Shift)
; https://www.sysnet.pe.kr/2/0/13110

.NET Framework: 2036. C# 11 - IntPtr/UIntPtr과 nint/nuint의 통합
; https://www.sysnet.pe.kr/2/0/13111

.NET Framework: 2037. C# 11 - 목록 패턴(List patterns)
; https://www.sysnet.pe.kr/2/0/13112

.NET Framework: 2038. C# 11 - Span 타입에 대한 패턴 매칭 (Pattern matching on ReadOnlySpan<char>)
; https://www.sysnet.pe.kr/2/0/13113

.NET Framework: 2042. C# 11 - 파일 범위 내에서 유효한 타입 정의 (File-local types)
; https://www.sysnet.pe.kr/2/0/13117

.NET Framework: 2045. C# 11 - 메서드 매개 변수에 대한 nameof 지원
; https://www.sysnet.pe.kr/2/0/13122

.NET Framework: 2046. C# 11 - 멤버(속성/필드)에 지정할 수 있는 required 예약어 추가
; https://www.sysnet.pe.kr/2/0/13123

.NET Framework: 2048. C# 11 - 구조체 필드의 자동 초기화(auto-default structs)
; https://www.sysnet.pe.kr/2/0/13125

.NET Framework: 2049. C# 11 - 정적 메서드에 대한 delegate 처리 시 cache 적용
; https://www.sysnet.pe.kr/2/0/13126

.NET Framework: 2102. C# 11 - ref struct/ref field를 위해 새롭게 도입된 scoped 예약어
; https://www.sysnet.pe.kr/2/0/13276




C# 11 - 목록 패턴(List patterns)

아래의 글에 보면,

C# 11 Preview: List patterns
; https://devblogs.microsoft.com/dotnet/early-peek-at-csharp-11-features/#c-11-preview-list-patterns

C# 11에 새롭게 추가한 목록 패턴 매칭을 소개합니다. 정말이지, 마이크로소프트가 C#의 패턴 매칭에 진심이군요. ^^ 처음 도입된 C# 7 이후로 꾸준히 패턴 매칭 문법을 보완하더니,

C# 7 - 기본 패턴 매칭 기능 추가
C# 8 - switch 식, 속성 패턴, 튜플 패턴, 위치 패턴 추가
C# 9 - "Type Patterns", "Relational Patterns", "Pattern Combinators", "Parenthesized Patterns" 추가
C# 10 - 속성 패턴 개선

C# 11에는 또다시 목록 패턴까지 추가했습니다. 간단하게 코드 먼저 볼까요? ^^

int[] arr1 = { 1, 2, 10 };
int[] arr2 = { 1, 2, 5, 10 };
int[] arr3 = { 1, 2, 5, 6, 7, 8, 9, 10 };

int[] arr4 = { 1, 3 };
int[] arr5 = { 1, 3, 6 };
int[] arr6 = { 2, 3 };

Console.WriteLine(CheckSwitch(arr1)); // 1
Console.WriteLine(CheckSwitch(arr2)); // 1
Console.WriteLine(CheckSwitch(arr3)); // 1

Console.WriteLine(CheckSwitch(arr4)); // 2
if (arr5 is [1, ..])
{
    Console.WriteLine(3); // 3
}
Console.WriteLine(CheckSwitch(arr6)); // 50

static int CheckSwitch(int[] values)
    => values switch
    {
        [1, 2, .., 10] => 1,
        [1, _] => 2,
        [1, ..] => 3,
        [..] => 50
    };

보시면, 목록의 패턴 매칭을 돕기 위해 그 자체에 슬라이스 패턴(..)과 무시 패턴(_)을 함께 제공하고 있습니다. 단지, switch 식의 경우엔 is와는 달리 그 특성상 "default" 경우를 위해 단독 슬라이스 패턴인 "[..]"을 마지막에 하나 제공해야 한다는 정도의 (없으면 경고를 주는) 추가 규칙이 있습니다.

List<int> list1 = new List<int>(new[] { 1, 3 });
Console.WriteLine(CheckSwitch(list1)); // 3

static int CheckSwitch(int[] values)
    => values switch
    {
        [1, 2, .., 10] => 1,
        [1, _] => 2,
        [1, ..] => 3,
        [..] => 50 // 없으면 warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
    };

static int CheckSwitch(List<int> values)
    => values switch
    {
        [1, 2] => 1,
        [1, _] => 2,
        [..] => 3, // 없으면 warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
    };

또한, 조건이 겹치는 패턴을 사용해도 오류가 발생합니다. 일례로 아래는 조건식 자체는 다르지만 결국 같은 조건에 해당하므로 컴파일 오류가 발생합니다.

static int CheckSwitch(IList values)
{
    switch (values)
    {
        // error CS8120: The switch case is unreachable. It has already been handled by a previous case or it is impossible to match.
        case [_, .., 1]: return 1;
        case [.., _, 1]: return 2;
    }

    return 0;
}

반면, 아래는 2개의 조건이 확실하게 다르므로 잘 동작합니다.

IList<int> list1 = new List<int>(new[] { 1, 1, 2, 3 });
Console.WriteLine(CheckSwitch(list1)); // 1

IList<int> list2 = new List<int>(new[] { 5, 6, 1, 9 });
Console.WriteLine(CheckSwitch(list2)); // 2

static int CheckSwitch(IList values)
{
    const int condition = 1;

    switch (values)
    {
        case [_, 1, ..]: return 1;
        case [.., condition, _]: return 2;
    }

    return 0;
}

나아가 기존의 패턴 문법과 병용한다면 좀 더 풍부한 조건을 만들 수 있습니다.

ArrayList arr1 = new ArrayList(new[] { 1, 3, 5 });
Console.WriteLine(CheckSwitch(arr1)); // 1

ArrayList arr2 = new ArrayList(new[] { 1, 10, 5 });
Console.WriteLine(CheckSwitch(arr2)); // 1

static int CheckSwitch(ArrayList values)
    => values switch
        {
            [_, >= 3, ..] or [_, <= 10, ..] => 1,
            [..] => -1,
        };

List<int[]> list = new List<int[]>(new[] { new[] { 1, 10, 5 } });
Console.WriteLine(CheckSwitch(list)); // 1

static int CheckSwitch(List<int[]> values)
    => values switch
    {
        [ [1, .., 5] ] => 1,
        [..] => -1,
    };

목록 패턴을 위한 대상은 다음의 요건만 갖추면 어떤 객체든 올 수 있습니다.

  1. Count 또는 Length 속성 제공
  2. indexer 속성 제공

대개의 경우, 위와 같은 조건은 IList 인터페이스를 구현하면 만족하는데요, 어쨌든 최대한 간단하게 위의 목록을 만족하는 클래스는 이런 식으로 정의할 수 있습니다.

NaturalNumber list = new NaturalNumber();
if (list is [1, .., Int32.MaxValue])
{
    Console.WriteLine(true); // True
}

public class NaturalNumber
{
    int _length = Int32.MaxValue;

    public int this[int index]
    {
        get { return index + 1; }
    }

    public int Length => _length;

    // 또는,
    // public int Count => _length;
}

마지막으로, 패턴 내의 슬라이스 기호(..)로 대표되는 결과를 변수로 받아오는 것도 가능합니다.

// 예제 코드: https://devblogs.microsoft.com/dotnet/early-peek-at-csharp-11-features/

public static string CaptureSlice(int[] values)
    => values switch
    {
        [1, .. var middle, _] => $"Middle {String.Join(", ", middle)}",
        [.. var all] => $"All {String.Join(", ", all)}"
    };

사용자 타입에서 저렇게 슬라이스 기호를 이용한 변수를 사용하려면 그에 따라 System.Range를 받아들이는 indexer를 함께 구현해야 합니다.

NaturalNumber list = new NaturalNumber();

if (list is [1, .. var middle, Int32.MaxValue])
{
    foreach (var elem in middle.Take(10))
    {
        Console.Write($"{elem},"); // 출력 결과: 2,3,4,5,6,7,8,9,10,11,
    }
}

public class NaturalNumber
{
    int _length = Int32.MaxValue;

    public int this[int index]
    {
        get { return index + 1; }
        set { }
    }

    public IEnumerable<int> this[System.Range range]
    {
        get
        {
            int startPos = range.Start.Value + 1;
            int endPos = _length - range.End.Value - startPos + 1;

            if (range.End.IsFromEnd == false)
            {
                endPos = range.End.Value - startPos + 1;
            }

            return Enumerable.Range(startPos, endPos);
        }
    }

    public int Length => _length;

    // 또는,
    // public int Count => Int32.MaxValue;
}

한 가지 제약이라면, 슬라이스로 지정한 영역을 변수로 받아오는 것은 (and는 가능하지만) not, or 패턴이 함께 지정된 목록 패턴에서는 사용할 수 없습니다.

// A variable may not be declared within a 'not' or 'or' pattern.
[_, >= 3, ..] or [_, <= 10, .. var rest] => 1,

따지고 보면 당연한 결과입니다. or 패턴의 경우 "Short-circuit Evaluation"이 발생하는 데다 변수로 받게 되는 결과가 해당 조건이 참이어서 받는 경우라면 상관없겠지만 다른 or 조건이 참이어서 받는 경우라면 가능하지 않는 상황일 수 있기 때문입니다. not 패턴 역시 그 조건이 아닌 목록에 대해 슬라이스 영역을 받게 되는 것이므로 undefined와 같은 상태가 될 수 있습니다.

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




C# 11 - 인터페이스 내에 정적 추상 메서드 정의 가능 (공식 문서, Static Abstract Members In Interfaces C# 10 Preview)
; https://www.sysnet.pe.kr/2/0/12814

C# 11 - 제네릭 타입의 특성 적용 (공식 문서, Generic attributes)
; https://www.sysnet.pe.kr/2/0/12839

C# 11 - 사용자 정의 checked 연산자 (공식 문서, Checked user-defined operators)
; https://www.sysnet.pe.kr/2/0/13099

C# 11 - shift 연산자 재정의에 대한 제약 완화 (공식 문서, Relaxing Shift Operator)
; https://www.sysnet.pe.kr/2/0/13100

C# 11 - IntPtr/UIntPtr과 nint/unint의 통합 (공식 문서, Numeric IntPtr)
; https://www.sysnet.pe.kr/2/0/13111

C# 11 - 새로운 연산자 ">>>" (Unsigned Right Shift) (공식 문서, Unsigned right shift operator)
; https://www.sysnet.pe.kr/2/0/13110

C# 11 - 원시 문자열 리터럴 (공식 문서, raw string literals)
; https://www.sysnet.pe.kr/2/0/13085

C# 11 - 문자열 보간 개선 2가지 (공식 문서, Allow new-lines in all interpolations)
; https://www.sysnet.pe.kr/2/0/13086

C# 11 - 목록 패턴 (공식 문서, List patterns)
; https://www.sysnet.pe.kr/2/0/13112

C# 11 - Span 타입에 대한 패턴 매칭 (공식 문서, Pattern matching on ReadOnlySpan<char>)
; https://www.sysnet.pe.kr/2/0/13113

C# 11 - Utf8 문자열 리터럴 지원 (공식 문서, Utf8 Strings Literals)
; https://www.sysnet.pe.kr/2/0/13096

C# 11 - ref struct에 ref 필드를 허용 (공식 문서, ref fields)
; https://www.sysnet.pe.kr/2/0/13015

C# 11 - 파일 범위 내에서 유효한 타입 정의 (공식 문서, File-local types)
; https://www.sysnet.pe.kr/2/0/13117

C# 11 - 메서드 매개 변수에 대한 nameof 지원 (공식 문서, nameof(parameter))
; https://www.sysnet.pe.kr/2/0/13122

C# 11 - 멤버(속성/필드)에 지정할 수 있는 required 예약어 추가 (공식 문서, Required members)
; https://www.sysnet.pe.kr/2/0/13123

C# 11 - 구조체 필드의 자동 초기화 (공식 문서, auto-default structs)
; https://www.sysnet.pe.kr/2/0/13125

C# 11 - 정적 메서드에 대한 delegate 처리 시 cache 적용 (공식 문서, Cache delegates for static method group)
; https://www.sysnet.pe.kr/2/0/13126

Language Feature Status
; https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md





[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 5/5/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2022-09-23 11시17분
정성태

1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13472정성태12/4/20232132C/C++: 164. Visual C++ - InterlockedCompareExchange128 사용 방법
13471정성태12/4/20232161Copilot - To enable GitHub Copilot, authorize this extension using GitHub's device flow
13470정성태12/2/20232468닷넷: 2178. C# - .NET 8부터 COM Interop에 대한 자동 소스 코드 생성 도입파일 다운로드1
13469정성태12/1/20232178닷넷: 2177. C# - (Interop DLL 없이) CoClass를 이용한 COM 개체 생성 방법파일 다운로드1
13468정성태12/1/20232172닷넷: 2176. C# - .NET Core/5+부터 달라진 RCW(Runtime Callable Wrapper) 대응 방식파일 다운로드1
13467정성태11/30/20232149오류 유형: 882. C# - Unhandled exception. System.Runtime.InteropServices.COMException (0x800080A5)파일 다운로드1
13466정성태11/29/20232372닷넷: 2175. C# - DllImport 메서드의 AOT 지원을 위한 LibraryImport 옵션
13465정성태11/28/20232103개발 환경 구성: 689. MSBuild - CopyToOutputDirectory가 "dotnet publish" 시에는 적용되지 않는 문제파일 다운로드1
13464정성태11/28/20232207닷넷: 2174. C# - .NET 7부터 UnmanagedCallersOnly 함수 export 기능을 AOT 빌드에 통합파일 다운로드1
13463정성태11/27/20232092오류 유형: 881. Visual Studio - NU1605: Warning As Error: Detected package downgrade
13462정성태11/27/20232140오류 유형: 880. Visual Studio - error CS0246: The type or namespace name '...' could not be found
13461정성태11/26/20232205닷넷: 2173. .NET Core 3/5+ 기반의 COM Server를 registry 등록 없이 사용하는 방법파일 다운로드1
13460정성태11/26/20232164닷넷: 2172. .NET 6+ 기반의 COM Server 내에 Type Library를 내장하는 방법파일 다운로드1
13459정성태11/26/20232190닷넷: 2171. .NET Core 3/5+ 기반의 COM Server를 기존의 regasm처럼 등록하는 방법파일 다운로드1
13458정성태11/26/20232194닷넷: 2170. .NET Core/5+ 기반의 COM Server를 tlb 파일을 생성하는 방법(tlbexp)
13457정성태11/25/20232145VS.NET IDE: 187. Visual Studio - 16.9 버전부터 추가된 "Display inline type hints" 옵션
13456정성태11/25/20232438닷넷: 2169. C# - OpenAI를 사용해 PDF 데이터를 대상으로 OpenAI 챗봇 작성 [1]파일 다운로드1
13455정성태11/25/20232323닷넷: 2168. C# - Azure.AI.OpenAI 패키지로 OpenAI 사용파일 다운로드1
13454정성태11/23/20232660닷넷: 2167. C# - Qdrant Vector DB를 이용한 Embedding 벡터 값 보관/조회 (Azure OpenAI) [1]파일 다운로드1
13453정성태11/23/20232205오류 유형: 879. docker desktop 설치 시 "Invalid JSON string. (Exception from HRESULT: 0x83750007)"
13452정성태11/22/20232289닷넷: 2166. C# - Azure OpenAI API를 이용해 사용자가 제공하는 정보를 대상으로 검색하는 방법파일 다운로드1
13451정성태11/21/20232420닷넷: 2165. C# - Azure OpenAI API를 이용해 ChatGPT처럼 동작하는 콘솔 응용 프로그램 제작파일 다운로드1
13450정성태11/21/20232242닷넷: 2164. C# - Octokit을 이용한 GitHub Issue 검색파일 다운로드1
13449정성태11/21/20232340개발 환경 구성: 688. Azure OpenAI 서비스 신청 방법
13448정성태11/20/20232616닷넷: 2163. .NET 8 - Dynamic PGO를 결합한 성능 향상파일 다운로드1
13447정성태11/16/20232480닷넷: 2162. ASP.NET Core 웹 사이트의 SSL 설정을 코드로 하는 방법
1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...