c# quick code to evaluate input using switch statement
Figure this would be handy to have, so i can re-use over and over again without too much thinking
A sample code to evaluate both string and date time using switch statement.
public CreditCardValidationResult Validate(string cardNumber, DateTime date)
{
int a = 100;
int total = a switch
{
var s when s > 0 && s < 100 => 10000,
var s when s > 101 && s < 200 => 20000
_ => -1
};
string cardType = cardNumber switch
{
var s when s.StartsWith("4") => "VISA",
var s when s.StartsWith("51") || s.StartsWith("52") || s.StartsWith("53")
|| s.StartsWith("54") || s.StartsWith("55") || s.StartsWith("37")
=> "MASTER",
var s when s.StartsWith("34") || s.StartsWith("37") => "AMEX",
_ => "unknown"
};
bool cardValid = date switch
{
var s when s >= DateTime.Now => true,
_ => false
};
return new CreditCardValidationResult(cardType, cardValid);
}
Comments