Skip to content

Instantly share code, notes, and snippets.

@dcagnetta
Last active March 18, 2020 11:48
Show Gist options
  • Save dcagnetta/87bd08774073a6e947b4051d819b732f to your computer and use it in GitHub Desktop.
Save dcagnetta/87bd08774073a6e947b4051d819b732f to your computer and use it in GitHub Desktop.
Switch Expressions with Tuples
public class Program
{
public static void Main()
{
Console.WriteLine(PeakTimeCalculator(DateTime.Now, true));
Console.WriteLine(PeakTimeCalculator(DateTime.Now.AddMonths(3), false));
}
// Calculates costs based on month and whether or not its peak set
public static decimal PeakTimeCalculator(DateTime date, bool isPeak) =>
(date.Month, isPeak) switch
{
(1, true) => 10.00m,
(2, true) => 5.00m,
(3,_) => 50.00m, // Matches true and false
(_,_) => 25.00m // Default
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment