-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
110 lines (90 loc) · 2.58 KB
/
Program.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
var x = 42;
/*
*
*/
class Thingy
{
public object SomeObjectWithStupidlyLongName;
public int Count;
public bool Flag;
public Thingy Nested;
}
class PatternMacher
{
Thingy GetThingy() => default;
void DoWorkVar(Thingy thingy)
{
// is { var pattern }
if (thingy is { SomeObjectWithStupidlyLongName: var x1 }
&& SomeCheck(x1)
&& SomeCheck(x1))
{
// Work
}
// is { type pattern }
if (thingy is { SomeObjectWithStupidlyLongName: string typeCheck })
{
// Work
}
// is { other patterns...var pattern...other patterns }
if (thingy is { Count: > 0, SomeObjectWithStupidlyLongName: var x3, Flag: true })
{
// Work
}
// is { other patterns...var pattern }
if (thingy is { Count: > 0, Flag: true, SomeObjectWithStupidlyLongName: var x2 })
{
// Work
}
// is { var pattern, var pattern... }
if (thingy is { Count: var a1, Flag: var a2, SomeObjectWithStupidlyLongName: var a3 })
{
// Work
}
// Psychopath code
if (thingy is { Nested: { Nested: { Nested: var x42 } } })
{
// Work
}
// Less Psychopath code
if (thingy is { Nested.Nested.Nested: var x41 })
{
// Work
}
}
void DoWorkNoVar(Thingy thingy)
{
// is { var pattern }
if (SomeCheck(thingy.SomeObjectWithStupidlyLongName)
&& SomeCheck(thingy.SomeObjectWithStupidlyLongName))
{
// Work
}
// is { type pattern }
if (thingy.SomeObjectWithStupidlyLongName is string typeCheck)
{
// Work
}
// is { other patterns...var pattern...other patterns }
if (thingy is { Count: > 0, Flag: true }
&& SomeCheck(thingy.SomeObjectWithStupidlyLongName))
{
// Work
}
// is { var pattern, var pattern... }
if (SomeCheck(thingy.Count)
&& SomeCheck(thingy.Flag)
&& SomeCheck(thingy.SomeObjectWithStupidlyLongName))
{
// Work
}
// Psychopath code
if (SomeCheck(thingy.Nested.Nested.Nested))
{
// Work
}
}
bool SomeCheck(object x) => false;
}