C# Programming > Miscellaneous
A fast way to verify credit cards in C# is to use the famous Luhn algorithm to validate the credit card number. The algorithm consists of simple number operations, making the code executable on desktop applications, ASP.NET, and even mobile applications.
As a note, not all valid credit cards can be verified with the Luhn algorithm. However the algorithm does work for many, if not most, major credit cards.
A little tip when validating credit card numbers in C# is to handle the information as a string instead of an integer. So the first step in the algorithm will be to make sure the input is a string format.
Once the credit card number is formatted, we can begin the Luhn algorithm to verify the number.
The first step of the process is to double alternating digits starting from the right. The results are stored in an array. Notice that the array will be half the length of the length of the credit card number.
The second step is to add up the digits in the resulting array. Remember not to add up the results themselves, but the digits. The easiest way to do this is by converting the numbers back to strings. The result will be a single integer result.
The third step is to add up the other alternating digits, this means the alternating digits starting from the second digit from the right. This time the digits are added up into a single integer result.
The final step is to add the results from step 2 and step 3. If the result is divisible by 10 (meaning result mod 10 equals 0), then the credit card is valid.
There are additional factors that can be checked to verify a credit card number in C#. For example you can check the length of the credit card number, or the prefix (the first few digits). There are tables on the internet that can even match those characteristics to specific types of credit cards.
Do your due diligence and make an educated decision by conducting a thorough credit card comparison before you decide.