publicstaticboolValidateCreditCardNumber(string creditCardNumber){
// Remove any spaces or dashes from the credit card number creditCardNumber = creditCardNumber.Replace(" ", "").Replace("-", "");
// Check if the credit card number contains only digitsif (!creditCardNumber.All(char.IsDigit))
{
returnfalse;
}
// Check if the credit card number is of valid length (13-16 digits)if (creditCardNumber.Length < 13 || creditCardNumber.Length > 16)
{
returnfalse;
}
// Check if the credit card number passes the Luhn algorithmint sum = 0;
bool alternate = false;
for (int i = creditCardNumber.Length - 1; i >= 0; i--)
{
int digit = int.Parse(creditCardNumber[i].ToString());
if (alternate)
{
digit *= 2;
if (digit > 9)
{
digit = (digit / 10) + (digit % 10);
}
}
sum += digit;
alternate = !alternate;
}
if (sum % 10 != 0)
{
returnfalse;
}
// If all checks pass, then the credit card number is validreturntrue;
}