Class Luhn
- Namespace
- LuhnDotNet
- Assembly
- LuhnDotNet.dll
A C# implementation of the Luhn algorithm.
public static class Luhn
- Inheritance
-
Luhn
- Inherited Members
Methods
ComputeLuhnCheckDigit(ReadOnlySpan<char>)
Computes the Luhn check digit
public static byte ComputeLuhnCheckDigit(this ReadOnlySpan<char> number)
Parameters
number
ReadOnlySpan<char>An identification number w/o check digit.
Returns
- byte
The calculated Luhn check digit.
Exceptions
- ArgumentException
number
is not valid. It contains none-numeric characters.
ComputeLuhnCheckDigit(string)
Computes the Luhn check digit.
public static byte ComputeLuhnCheckDigit(this string number)
Parameters
number
stringAn identification number w/o check digit.
Returns
- byte
The calculated Luhn check digit as a byte.
Exceptions
- ArgumentException
number
is not valid. It contains none-numeric characters.
ComputeLuhnNumber(ReadOnlySpan<char>)
Computes the Luhn number, which is a combination of the given number and the calculated check digit.
public static string ComputeLuhnNumber(this ReadOnlySpan<char> number)
Parameters
number
ReadOnlySpan<char>An identification number w/o check digit.
Returns
- string
The calculated Luhn number.
Exceptions
- ArgumentException
number
is not valid. It contains none-numeric characters.
ComputeLuhnNumber(string)
Computes the Luhn number, which is a combination of the given number and the calculated check digit.
public static string ComputeLuhnNumber(this string number)
Parameters
number
stringAn identification number w/o check digit.
Returns
- string
The calculated Luhn number.
Exceptions
- ArgumentException
number
is not valid. It contains none-numeric characters.
ConvertAlphaNumericToNumeric(string)
Converts an alphanumeric string to a numeric string.
public static string ConvertAlphaNumericToNumeric(this string alphaNumeric)
Parameters
alphaNumeric
stringThe alphanumeric string to convert.
Returns
- string
A numeric string where each letter in the input string is replaced by its decimal ASCII value minus 55.
Remarks
This method iterates over each character in the input string. If the character is a letter, it is replaced by its decimal ASCII value minus 55. If the character is a digit, it is left unchanged.
Exceptions
- ArgumentException
The
alphaNumeric
contains a character that is neither a letter nor a digit.
IsValid(ReadOnlySpan<char>)
Checks whether the Luhn Number is valid
[Obsolete("Use the IsValidLuhnNumber method instead.", false)]
public static bool IsValid(this ReadOnlySpan<char> luhnNumber)
Parameters
luhnNumber
ReadOnlySpan<char>An identification number w/ check digit (Luhn Number).
Returns
Remarks
The check digit must be at the end of the luhnNumber
(on the right side).
Exceptions
- ArgumentException
luhnNumber
is not valid. It contains none-numeric characters.
IsValid(ReadOnlySpan<char>, byte)
Checks whether the concatenation of number and corresponding Luhn check digit is valid
[Obsolete("Use the IsValidLuhnCheckDigit method instead.", false)]
public static bool IsValid(this ReadOnlySpan<char> number, byte checkDigit)
Parameters
number
ReadOnlySpan<char>Identification number w/o Luhn check digit
checkDigit
byteThe Luhn check digit
Returns
Exceptions
- ArgumentException
number
is not valid. It contains none-numeric characters.- ArgumentOutOfRangeException
The
checkDigit
value is greater than 9. ThecheckDigit
value must be between 0 and 9.
IsValid(string)
Checks whether the Luhn Number is valid
[Obsolete("Use the IsValidLuhnNumber method instead.", false)]
public static bool IsValid(this string luhnNumber)
Parameters
luhnNumber
stringAn identification number w/ check digit (Luhn Number).
Returns
Remarks
The check digit must be at the end of the luhnNumber
(on the right side).
Exceptions
- ArgumentException
luhnNumber
is not valid. It contains none-numeric characters.
IsValid(string, byte)
Checks whether the concatenation of number and corresponding Luhn check digit is valid
[Obsolete("Use the IsValidLuhnCheckDigit method instead.", false)]
public static bool IsValid(this string number, byte checkDigit)
Parameters
Returns
Exceptions
- ArgumentException
number
is not valid. It contains none-numeric characters.- ArgumentOutOfRangeException
The
checkDigit
value is greater than 9. ThecheckDigit
value must be between 0 and 9.
IsValidLuhnCheckDigit(byte, ReadOnlySpan<char>)
Checks whether the concatenation of number and corresponding Luhn check digit is valid
public static bool IsValidLuhnCheckDigit(this byte checkDigit, ReadOnlySpan<char> number)
Parameters
checkDigit
byteThe Luhn check digit
number
ReadOnlySpan<char>Identification number w/o Luhn check digit
Returns
Exceptions
- ArgumentException
number
is not valid. It contains none-numeric characters.- ArgumentOutOfRangeException
The
checkDigit
value is greater than 9. ThecheckDigit
value must be between 0 and 9.
IsValidLuhnCheckDigit(byte, string)
Checks whether the concatenation of number and corresponding Luhn check digit is valid
public static bool IsValidLuhnCheckDigit(this byte checkDigit, string number)
Parameters
Returns
Exceptions
- ArgumentException
number
is not valid. It contains none-numeric characters.- ArgumentOutOfRangeException
The
checkDigit
value is greater than 9. ThecheckDigit
value must be between 0 and 9.
IsValidLuhnNumber(ReadOnlySpan<char>)
Checks whether the Luhn Number is valid
public static bool IsValidLuhnNumber(this ReadOnlySpan<char> luhnNumber)
Parameters
luhnNumber
ReadOnlySpan<char>An identification number w/ check digit (Luhn Number).
Returns
Remarks
The check digit must be at the end of the luhnNumber
(on the right side).
Exceptions
- ArgumentException
luhnNumber
is not valid. It contains none-numeric characters.
IsValidLuhnNumber(string)
Checks whether the Luhn Number is valid
public static bool IsValidLuhnNumber(this string luhnNumber)
Parameters
luhnNumber
stringAn identification number w/ check digit (Luhn Number).
Returns
Remarks
The check digit must be at the end of the luhnNumber
(on the right side).
Exceptions
- ArgumentException
luhnNumber
is not valid. It contains none-numeric characters.