Favicon

Relational patterns

Peponi1/8/20252m

C#
SyntaxKeywordPattern><=

1. Introduction

다음 C# 식과 문은 패턴을 지원한다.

여기서는 식 결과를 지정된 상수와 비교할 수 있는 관계형 패턴을 설명한다. 관계형 패턴에는 다음 형식을 사용할 수 있다.

2. 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 switch
{
    < 0 => "foo < 0",
    < 5 => "foo < 5",
    < 10 => "foo < 10",
    _ => string.Empty
});
 
/* output:
foo < 10
*/
switch 문
int foo = 7;
string writeString = string.Empty;
 
switch (foo)
{
    case < 0:
        writeString = "foo < 0";
        break;
 
    case < 5:
        writeString = "foo < 5";
        break;
 
    case < 10:
        writeString = "foo < 10";
        break;
}
 
Console.WriteLine(writeString);
 
/* output:
foo < 10
*/

3. 참조 자료