Skip to content

Instantly share code, notes, and snippets.

@NineMvp
Last active October 19, 2016 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NineMvp/71c01d0ee20e692d36b50b3e8fe79d30 to your computer and use it in GitHub Desktop.
Save NineMvp/71c01d0ee20e692d36b50b3e8fe79d30 to your computer and use it in GitHub Desktop.
Pattern Matching
static bool PatternMatching(object x) {
// 'is' pattern variable
if (x is int i && i > 0) return true;
if (x is string k && k.Trim().Length > 0) return true;
if (x is person p && p != null) return true;
else return false;
// switch statement with pattern 'case' and 'when'
switch (x) {
case int a when a > 0: return true;
case string t when t.Trim().Length > 0: return true;
case person m when m != null: return true;
default: return false;
}
}
//demo class
class person {
public string fname { get; set; }
public string sname { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment