C# Reference ยท Numbers and Math
C# Convert class
Reference for the C# Convert class: convert between numbers, strings, bools, bytes and base-2/8/16, with rounding, null and overflow rules explained.
System.Convert is a static class with methods that convert between the base types: numbers, bool, char, string, DateTime, byte[] and more. Its method names follow the pattern ToXxx (ToInt32, ToDouble, ToString, ToBoolean) and each is overloaded for every source type.
Three behaviors set Convert apart from casts and from Parse. Converting a floating-point number to an integer rounds to the nearest value (ties go to even) instead of truncating. Converting null to a number or bool gives 0 or false instead of throwing. And out-of-range values always throw OverflowException, regardless of any unchecked context.
int a = Convert.ToInt32(2.5); // 2 (banker's rounding) int b = (int)2.9; // 2 (a cast truncates) int c = Convert.ToInt32("42"); // 42 (parses the string)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static int Convert.ToInt32(double value) // also string, bool, char, decimal, long, object ... public static int Convert.ToInt32(string? value, int fromBase) public static double Convert.ToDouble(string? value, IFormatProvider? provider) public static bool Convert.ToBoolean(string? value) public static string Convert.ToString(int value, int toBase) public static object Convert.ChangeType(object? value, Type conversionType, IFormatProvider? provider) public static string Convert.ToBase64String(byte[] inArray) public static byte[] Convert.FromBase64String(string s) public static string Convert.ToHexString(byte[] inArray) public static byte[] Convert.FromHexString(string s)
| Name | Description |
|---|---|
value |
The value to convert. null is accepted for most string and object overloads. |
fromBase / toBase |
The number base: 2, 8, 10 or 16. Anything else throws ArgumentException. |
provider |
Supplies culture rules for text. Pass CultureInfo.InvariantCulture for culture-neutral data. |
conversionType |
The target type for ChangeType. |
Returns: the converted value in the requested type.
Errors: FormatException when text is not a valid number or bool (and for an empty string), OverflowException when the result does not fit, InvalidCastException when no conversion exists (for example Convert.ToInt32(DateTime.Now)), ArgumentNullException for Convert.FromBase64String(null).
Usage notes
- Rounding differs from a cast.
Convert.ToInt32(2.5)is2because ties round to the nearest even number;(int)2.5truncates and also gives2, but3.5converts to4withConvertand3with a cast. UseMath.Roundwith an explicit mode when the rule matters. nullis forgiving, empty is not.Convert.ToInt32((string?)null)returns0, butConvert.ToInt32("")throwsFormatException.- Prefer
TryParsefor user input.Convert.ToInt32("abc")throws, and exceptions are slow when failures are common. Useint.TryParsefor text that may be wrong. - Overflow always throws.
Convertchecks ranges even insideuncheckedcode, unlike a cast. - Cultures. The overloads without a provider use the current culture. The runner here uses the invariant culture, but on a real machine the same call can read
1,5differently. Pass a provider for stored or transmitted data. - Hex and Base64.
ToHexString,FromHexString(.NET 5+) andToBase64Stringare the standard way to show bytes as text.FromHexStringis case-insensitive. - Boxing. The
objectoverloads andChangeTypebox value types. Use the typed overloads in hot paths. Convert.ToChar(int)converts a code unit; it does not handle characters above U+FFFF.
Related
num-parseandnum-tostringstr-base64andstr-hextype-conversions- Tutorial: Type conversion