Skip to content

Instantly share code, notes, and snippets.

@savaged
Last active January 31, 2020 13:39
Show Gist options
  • Save savaged/7eba740c0a82e4f353bc22faeea3b6b6 to your computer and use it in GitHub Desktop.
Save savaged/7eba740c0a82e4f353bc22faeea3b6b6 to your computer and use it in GitHub Desktop.
Simple C# 8.0 pattern matching switch example
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