Last active
October 19, 2016 15:17
-
-
Save NineMvp/71c01d0ee20e692d36b50b3e8fe79d30 to your computer and use it in GitHub Desktop.
Pattern Matching
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
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