-
-
Save rgarner/1206e3a9b29139a429234fb16dfcb6a2 to your computer and use it in GitHub Desktop.
Proposed pattern matching in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From: https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/ | |
switch(shape) | |
{ | |
case Circle c: | |
WriteLine($"circle with radius {c.Radius}"); | |
break; | |
case Rectangle s when (s.Length == s.Height): | |
WriteLine($"{s.Length} x {s.Height} square"); | |
break; | |
case Rectangle r: | |
WriteLine($"{r.Length} x {r.Height} rectangle"); | |
break; | |
default: | |
WriteLine("<unknown shape>"); | |
break; | |
case null: | |
throw new ArgumentNullException(nameof(shape)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match (shape) | |
{ | |
Circle c => WriteLine($"circle with radius {c.Radius}"); | |
Rectangle s when (s.Length == s.Height) { //statement block example | |
WriteLine($"{s.Length} x {s.Height} square"); | |
} | |
Rectangle r => WriteLine($"{r.Length} x {r.Height} rectangle"); | |
default => WriteLine("<unknown shape>"); | |
null => throw new ArgumentNullException(nameof(shape)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment