Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Created June 19, 2018 11:23
Show Gist options
  • Save christiannagel/21f72b469a3b9d202e58b829738ce0a9 to your computer and use it in GitHub Desktop.
Save christiannagel/21f72b469a3b9d202e58b829738ce0a9 to your computer and use it in GitHub Desktop.
C# 7 pattern matching with the switch statement and type patterns with a filter
static string M1(Shape shape)
{
switch (shape)
{
case Shape s when s.Size.height > 100:
return $"large shape with size {s.Size} at position {s.Position}";
case Ellipse e:
return $"Ellipse with size {e.Size} at position {e.Position}";
case Rectangle r:
return $"Rectangle with size {r.Size} at position {r.Position}";
default:
return "another shape";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment