Microsoft MVP성태의 닷넷 이야기
.NET Framework: 2037. C# 11 - 목록 패턴(List patterns) [링크 복사], [링크+제목 복사],
조회: 8546
글쓴 사람
정성태 (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분
정성태

... 31  32  33  34  35  36  [37]  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12721정성태7/20/20218072오류 유형: 738. The trust relationship between this workstation and the primary domain failed. - 세 번째 이야기
12720정성태7/19/20217424Linux: 43. .NET Core/5+ 응용 프로그램의 Ubuntu (Debian) 패키지 준비
12719정성태7/19/20216613오류 유형: 737. SharePoint 설치 시 "0x800710D8 The object identifier does not represent a valid object." 오류 발생
12718정성태7/19/20217201개발 환경 구성: 581. Windows에서 WSL로 파일 복사 시 root 소유권으로 적용되는 문제파일 다운로드1
12717정성태7/18/20217133Windows: 195. robocopy에서 파일의 ADS(Alternate Data Stream) 정보 복사를 제외하는 방법
12716정성태7/17/20218021개발 환경 구성: 580. msbuild의 Exec Task에 robocopy를 사용하는 방법파일 다운로드1
12715정성태7/17/20219577오류 유형: 736. Windows - MySQL zip 파일 버전의 "mysqld --skip-grant-tables" 실행 시 비정상 종료 [1]
12714정성태7/16/20218376오류 유형: 735. VCRUNTIME140.dll, MSVCP140.dll, VCRUNTIME140.dll, VCRUNTIME140_1.dll이 없어 exe 실행이 안 되는 경우
12713정성태7/16/20218912.NET Framework: 1077. C# - 동기 방식이면서 비동기 규약을 따르게 만드는 Task.FromResult파일 다운로드1
12712정성태7/15/20218337개발 환경 구성: 579. Azure - 리눅스 호스팅의 Site Extension 제작 방법
12711정성태7/15/20218703개발 환경 구성: 578. Azure - Java Web App Service를 위한 Site Extension 제작 방법
12710정성태7/15/202110483개발 환경 구성: 577. MQTT - emqx.io 서비스 소개
12709정성태7/14/20217027Linux: 42. 실행 중인 docker 컨테이너에 대한 구동 시점의 docker run 명령어를 확인하는 방법
12708정성태7/14/202110480Linux: 41. 리눅스 환경에서 디스크 용량 부족 시 원인 분석 방법
12707정성태7/14/202177758오류 유형: 734. MySQL - Authentication method 'caching_sha2_password' not supported by any of the available plugins.
12706정성태7/14/20218899.NET Framework: 1076. C# - AsyncLocal 기능을 CallContext만으로 구현하는 방법 [2]파일 다운로드1
12705정성태7/13/20219073VS.NET IDE: 168. x64 DLL 프로젝트의 컨트롤이 Visual Studio의 Designer에서 보이지 않는 문제 - 두 번째 이야기
12704정성태7/12/20218222개발 환경 구성: 576. Azure VM의 서비스를 Azure Web App Service에서만 접근하도록 NSG 설정을 제한하는 방법
12703정성태7/11/202113906개발 환경 구성: 575. Azure VM에 (ICMP) ping을 허용하는 방법
12702정성태7/11/20219022오류 유형: 733. TaskScheduler에 등록된 wacs.exe의 Let's Encrypt 인증서 업데이트 문제
12701정성태7/9/20218703.NET Framework: 1075. C# - ThreadPool의 스레드는 반환 시 ThreadStatic과 AsyncLocal 값이 초기화 될까요?파일 다운로드1
12700정성태7/8/20219065.NET Framework: 1074. RuntimeType의 메모리 누수? [1]
12699정성태7/8/20217875VS.NET IDE: 167. Visual Studio 디버깅 중 GC Heap 상태를 보여주는 "Show Diagnostic Tools" 메뉴 사용법
12698정성태7/7/202111836오류 유형: 732. Windows 11 업데이트 시 3% 또는 0%에서 다운로드가 멈춘 경우
12697정성태7/7/20217684개발 환경 구성: 574. Windows 11 (Insider Preview) 설치하는 방법
12696정성태7/6/20218308VC++: 146. 운영체제의 스레드 문맥 교환(Context Switch)을 유사하게 구현하는 방법파일 다운로드2
... 31  32  33  34  35  36  [37]  38  39  40  41  42  43  44  45  ...