Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Last active August 26, 2021 07:11
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 ankitvijay/e3a3d36b957ea3e4171c9c5c2da7153f to your computer and use it in GitHub Desktop.
Save ankitvijay/e3a3d36b957ea3e4171c9c5c2da7153f to your computer and use it in GitHub Desktop.
Switch Expression Test
using System;
Console.WriteLine("GetBoolean 1:" + (GetBoolean1("Nah") == null));
Console.WriteLine("GetBoolean 2:" + (GetBoolean2("Nah") == null));
static Boolean? GetBoolean2(string boolString)
{
return boolString switch
{
"Yes" => Boolean.Yes,
"No" => Boolean.No,
_ => default
};
}
static Boolean? GetBoolean1(string boolString)
{
switch (boolString)
{
case "Yes":
return Boolean.Yes;
case "No":
return Boolean.No;
default:
return default;
}
}
enum Boolean
{
Yes,
No
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment