Last active
March 18, 2020 11:48
-
-
Save dcagnetta/87bd08774073a6e947b4051d819b732f to your computer and use it in GitHub Desktop.
Switch Expressions with Tuples
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
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