Last active
January 31, 2020 13:39
-
-
Save savaged/7eba740c0a82e4f353bc22faeea3b6b6 to your computer and use it in GitHub Desktop.
Simple C# 8.0 pattern matching switch example
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
using System; | |
namespace Patterns.Switch.Lib | |
{ | |
public class GreetingService | |
{ | |
public string GetGreeting(string? name) | |
{ | |
var value = name switch | |
{ | |
var s when (string.IsNullOrEmpty(s)) => "Who?", | |
"Bruce" => GetFullGreeting("Good day,", name), | |
"Buck" => GetFullGreeting("Howdy,", name), | |
"David" => GetFullGreeting("Hello,", name), | |
_ => GetFullGreeting("Hey,", name) | |
}; | |
return value; | |
} | |
private string GetFullGreeting(string salutation, string? name) | |
{ | |
var value = $"{salutation} {name}!"; | |
return value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment