-
Notifications
You must be signed in to change notification settings - Fork 48
/
PatternMatchingSample.cs
57 lines (49 loc) · 1.52 KB
/
PatternMatchingSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
namespace CSharp9Sample
{
internal static class PatternMatchingSample
{
public static void MainTest()
{
var person = new Person();
//string.IsNullOrEmpty(person.Description), or
if (person.Description is null or { Length: 0 })
{
Console.WriteLine($"{nameof(person.Description)} is IsNullOrEmpty");
}
// !string.IsNullOrEmpty(person.Name), and
if (person.Name is not null and { Length: > 0 })
{
if (person.Name[0] is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.' or ',')
{
}
}
// not
if (person.Name is not null)
{
}
// switch
switch (person.Age)
{
case >= 0 and <= 3:
Console.WriteLine("baby");
break;
case > 3 and < 14:
Console.WriteLine("child");
break;
case > 14 and < 22:
Console.WriteLine("youth");
break;
case > 22 and < 60:
Console.WriteLine("Adult");
break;
case >= 60 and <= 500:
Console.WriteLine("Old man");
break;
case > 500:
Console.WriteLine("monster");
break;
}
}
}
}