Skip to content

Instantly share code, notes, and snippets.

@PradeepLoganathan
Created June 13, 2017 10:31
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 PradeepLoganathan/9b5b1cf83e3310560b50fe9e927c5123 to your computer and use it in GitHub Desktop.
Save PradeepLoganathan/9b5b1cf83e3310560b50fe9e927c5123 to your computer and use it in GitHub Desktop.
//Const pattern check
public static void IsPattern(object o)
{
    if (o is null) Console.WriteLine("it's a const pattern");
    //...
}
public static void IsPattern(object o)
{
    if (o is null) throw new ArgumentNullException(nameof(o));
if (o is 42) Console.WriteLine("it's 42");
    //...
}
//Type Pattern
if (o is int i) Console.WriteLine($"it's a type pattern
if (o is Person p) Console.WriteLine($"it's a person: {p.FirstName}");
//var pattern
if (o is var x) Console.WriteLine($"it's a var pattern with the type {x?.GetType()?.Name}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment