Pattern

C#/Language

C# - Language - List patterns

Introduction 다음 C# 식과 문은 패턴을 지원한다. is 식 switch 식 switch 문 여기서는 C# 11부터 지원되는 목록 패턴에 대해 설명한다. Example 목록 패턴은 배열, 컬렉션에 대해 특정 시퀀스와 매칭하는 기능을 제공한다. List ints = [-1, 2, 3]; // 기본 사용 Console.WriteLine(ints switch { [-1, 2, 3] => true, _ => false }); /* output: True */ // 패턴 중첩 Console.WriteLine(ints switch { [ mid, _ => false }); /* output: 2 */ 목록 패턴은 범위 연산자 (..)를 통한 부분 매칭을 지원한다..

C#/Language

C# - Language - Discard, var pattern

Introduction 다음 C# 식과 문은 패턴을 지원한다. is 식 switch 식 switch 문 여기서는 다음 패턴을 설명한다. 무시 (_) 패턴 : null을 포함한 모든 식을 매칭한다. var 패턴 : null을 포함한 모든 식을 매칭하고 지역 변수에 할당한다. _ 패턴 _ 패턴은 switch 식에만 사용이 가능하고, switch 문의 default와 같은 기능을 한다. switch 식에서 _을 사용하지 않고 모든 경우를 커버하지 못하면 컴파일러는 경고를 생성한다. (CS8509) int? foo = null; Console.WriteLine(foo switch { 1 => 1, 2 => 2, _ => "Not recognized" }); /* output: Not recognized */ v..

C#/Language

C# - Language - Property, positional patterns

Introduction 다음 C# 식과 문은 패턴을 지원한다. is 식 switch 식 switch 문 여기서는 다음 패턴을 설명한다. 속성 패턴 : 각 패턴에 대해 속성, 필드를 매칭 위치 패턴 : 식을 분해하고 패턴 매칭 속성 패턴 속성 패턴은 중괄호 { } 를 이용하여 매칭할 속성을 지정한다. public record Foo(int A, int B); var foo = new Foo(1, 2); Console.WriteLine(foo switch { { A: "A "B 0, B: > 0 } => "A > 0, B > 0", _ => "Not recognized" }); /* output: A > 0, B > 0 */ 속성 패..

C#/Language

C# - Language - Logical patterns

Introduction 다음 C# 식과 문은 패턴을 지원한다. is 식 switch 식 switch 문 여기서는 각 패턴을 조합할 수 있는 논리 패턴을 설명한다. not (부정 패턴) not 패턴은 지정된 식과 일치하지 않는 경우 true를 반환한다. int? foo = 7; if (foo is not int) Console.WriteLine("foo is not int"); else if (foo is int) Console.WriteLine("foo is int"); /* output: foo is int */ and (결합 패턴) and 패턴은 지정된 모든 식이 일치하는 경우 true를 반환한다. int foo = 7; Console.WriteLine(foo switch { "foo <..

C#/Language

C# - Language - Relational patterns

Introduction 다음 C# 식과 문은 패턴을 지원한다. is 식 switch 식 switch 문 여기서는 식 결과가 지정된 상수와 비교할 수 있는 관계형 패턴을 설명한다. 관계형 패턴에는 다음 형식을 사용할 수 있다. 정수, 부동 소수점 char enum Example // is 식 int foo = 7; if (foo is < 0) Console.WriteLine("foo < 0"); else if (foo is < 5) Console.WriteLine("foo < 5"); else if (foo is < 10) Console.WriteLine("foo < 10"); /* output: foo < 10 */ // switch 식 int foo = 7; Console.WriteLine(foo sw..

C#/Language

C# - Language - Constant pattern

Introduction 다음 C# 식과 문은 패턴을 지원한다. is 식 switch 식 switch 문 여기서는 식 결과가 지정된 상수와 같은지 확인할 수 있는 상수 패턴을 설명한다. 상수 패턴에는 상수 형식으로 변환할 수 있는 형식을 사용할 수 있다. 정수, 부동 소수점 char string bool enum const 필드 또는 로컬 변수의 이름 Span, ReadOnlySpan (C# 11) null Example // is 식 int foo = 7; if (foo is 7) Console.WriteLine("foo is 7"); else Console.WriteLine("foo is not 7"); /* output: foo is 7 */ // switch 식 int foo = 7; Console..

C#/Language

C# - Language - Type, declaration patterns

Introduction 다음 C# 식과 문은 패턴을 지원한다. is 식 switch 식 switch 문 여기서는 다음 패턴을 설명한다. 형식 패턴 : 형식 확인 선언 패턴 : 형식 확인 및 성공 시 결과를 변수에 할당 특정 객체가 T 형식임을 확인하는 경우, 아래 형식 조건 중 하나를 만족하면 true가 반환된다. T or T? T의 파생 형식 T의 인터페이스 구현 T 형식으로 boxing 또는 unboxing 가능 형식 패턴 식의 형식을 확인하려는 경우 다음과 같이 형식 패턴을 사용할 수 있다. namespace TypePattern { public interface Common { } public class Foo : Common { } public class Bar : Common { } } // ..

Toy Project

C# library - Peponi.Core

1. Instruction This package is under MIT License. GitHub : Peponi Blog : Peponi Instruction & API information is on following section 1.1. About Peponi.Core Peponi.Core is a package for common usage of peponi library. Included contents are: 1. Design pattern - Singleton 2. Utility - Helpers + Directory + Member + Process + Registry + Storage - Minidump 1.1.1. Peponi.Core license The MIT License ..

Peponi_
'Pattern' 태그의 글 목록