C# Convert
last modified October 16, 2026
In this article we show how to convert types in C# with the Convert
class.
Type conversion or typecasting refers to changing an entity of
one data type into another. There are two types of conversion: implicit and
explicit. Implicit type conversion is an automatic type conversion by the
compiler; it is done when no data can be lost, for instance from
int to long. Explicit conversions are done with a cast
or with conversion methods.
The static Convert class from the System namespace
converts a base data type to another base data type. Its methods check for
overflow and throw exceptions for invalid input.
C# conversion types
In the next example, we present three ways of data conversion.
int n = 12; string s = "15"; string res = n + s; Console.WriteLine(res); int res2 = int.Parse(s) + n; Console.WriteLine(res2); int res3 = Convert.ToInt32(s) + n; Console.WriteLine(res3);
The program defines an integer and a string. We add those two values.
string res = n + s;
When one operand of the + operator is a string, the compiler
converts the other operand to a string and concatenates them.
int res2 = int.Parse(s) + n;
We can use the int.Parse method to explicitly convert the string
to an integer and add two integers.
int res3 = Convert.ToInt32(s) + n;
Alternatively, we can use the Convert.ToInt32 method. The
Convert class contains methods for common type conversions.
$ dotnet run 1215 27 27
C# Convert.ToBoolean
The Convert.ToBoolean method converts a specified value to an
equivalent boolean value. Zero is converted to false, other numbers to true.
double n1 = 0.3;
int n2 = 3;
int n3 = 0;
int n4 = -1;
decimal d = 0.35m;
Console.WriteLine(Convert.ToBoolean(n1));
Console.WriteLine(Convert.ToBoolean(n2));
Console.WriteLine(Convert.ToBoolean(n3));
Console.WriteLine(Convert.ToBoolean(n4));
Console.WriteLine(Convert.ToBoolean(d));
Console.WriteLine(Convert.ToBoolean("True"));
In the program, we convert double, int, decimal, and string values to booleans.
A string must contain "True" or "False" in any case; otherwise, a
FormatException is thrown.
$ dotnet run True True False True True True
C# Convert.ToInt32 rounding and overflow
When converting a floating point number to an integer, Convert
rounds the value to the nearest integer; a midpoint value is rounded to the
nearest even number. A cast truncates the fractional part. Unlike a cast,
Convert throws an OverflowException if the value does
not fit into the target type; no checked context is needed.
double[] vals = [2.5, 3.5, -2.5, 2.6];
foreach (var v in vals)
{
Console.WriteLine($"{v}: Convert {Convert.ToInt32(v)}, cast {(int) v}");
}
double big = 3e9;
try
{
int n = Convert.ToInt32(big);
Console.WriteLine(n);
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
try
{
int n = Convert.ToInt32("2147483648");
Console.WriteLine(n);
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
We convert four values with Convert.ToInt32 and with a cast. Then
we try to convert a double and a string whose values exceed
int.MaxValue.
$ dotnet run 2.5: Convert 2, cast 2 3.5: Convert 4, cast 3 -2.5: Convert -2, cast -2 2.6: Convert 3, cast 2 Value was either too large or too small for an Int32. Value was either too large or too small for an Int32.
C# Convert with null values
The Convert methods that take a string or an object return the
default value of the target type for a null argument. In contrast,
int.Parse(null) throws an ArgumentNullException.
string? nullableString = null;
object? nullableObject = null;
int result1 = Convert.ToInt32(nullableString);
int result2 = Convert.ToInt32("42");
int result3 = Convert.ToInt32(nullableObject);
Console.WriteLine($"Null string to int: {result1}");
Console.WriteLine($"Valid string to int: {result2}");
Console.WriteLine($"Null object to int: {result3}");
Null values are converted to 0, while valid strings are converted to their integer equivalent. Note that this can hide missing data; a missing value and the value 0 cannot be distinguished.
$ dotnet run Null string to int: 0 Valid string to int: 42 Null object to int: 0
C# Convert.ToDecimal
The Convert.ToDecimal method converts a specified value to the
decimal type, which is suitable for high-precision calculations
such as financial applications.
using System.Globalization;
string price = "123.456";
int quantity = 5;
double rate = 0.075;
decimal priceDecimal = Convert.ToDecimal(price, CultureInfo.InvariantCulture);
decimal quantityDecimal = Convert.ToDecimal(quantity);
decimal rateDecimal = Convert.ToDecimal(rate);
decimal total = priceDecimal * quantityDecimal * (1 + rateDecimal);
Console.WriteLine($"Price: {priceDecimal:C}");
Console.WriteLine($"Quantity: {quantityDecimal}");
Console.WriteLine($"Tax rate: {rateDecimal:P1}");
Console.WriteLine($"Total with tax: {total:C}");
In this example, we convert a string, an integer, and a double to
decimal and calculate a total price with tax.
decimal priceDecimal = Convert.ToDecimal(price, CultureInfo.InvariantCulture);
When converting a string, we pass the culture whose number format the string uses. Without it, the current culture is used; in many cultures the comma is the decimal separator and "123.456" would be read differently or rejected.
Console.WriteLine($"Total with tax: {total:C}");
The C format specifier formats the value as currency of the
current culture; the value is rounded to two decimal places for display.
$ dotnet run Price: $123.46 Quantity: 5 Tax rate: 7.5% Total with tax: $663.58
C# Convert hexadecimal string
The Convert.ToHexString method converts an array of bytes to its
equivalent string representation encoded with uppercase hex characters. The
Convert.FromHexString method converts a string of hex characters
back to a byte array.
using System.Text; string msg = "an old falcon"; byte[] data = Encoding.ASCII.GetBytes(msg); string hexstr = Convert.ToHexString(data); Console.WriteLine(hexstr); Console.WriteLine(Convert.ToHexStringLower(data)); byte[] data2 = Convert.FromHexString(hexstr); Console.WriteLine(Encoding.ASCII.GetString(data2));
The program converts a message to a hexadecimal string and back. The
ToHexStringLower method, available since .NET 9, produces
lowercase hex characters.
$ dotnet run 616E206F6C642066616C636F6E 616e206f6c642066616c636f6e an old falcon
C# Convert.ToBase64String
The Convert.ToBase64String method converts an array of bytes to
its equivalent string representation encoded with base-64 digits. The
Convert.FromBase64String method converts a base-64 string back to
a byte array.
Base64 encoding is used to transfer binary data through channels that support only text, such as e-mail attachments, JSON, or data URLs.
using System.Text;
string msg = "an old falcon";
byte[] data = Encoding.ASCII.GetBytes(msg);
Console.WriteLine(BitConverter.ToString(data));
string base64 = Convert.ToBase64String(data);
Console.WriteLine(base64);
Console.WriteLine("---------------------");
byte[] data2 = Convert.FromBase64String(base64);
Console.WriteLine(BitConverter.ToString(data2));
Console.WriteLine(Encoding.ASCII.GetString(data2));
In the program, we convert a string to a byte array and the array to a Base64 string. Then we reverse the process and decode the Base64 string.
$ dotnet run 61-6E-20-6F-6C-64-20-66-61-6C-63-6F-6E YW4gb2xkIGZhbGNvbg== --------------------- 61-6E-20-6F-6C-64-20-66-61-6C-63-6F-6E an old falcon
C# Convert.ToDateTime
The Convert.ToDateTime method converts the specified string
representation of a date and time to an equivalent date and time value.
public static DateTime ToDateTime(string? value); public static DateTime ToDateTime(string? value, IFormatProvider? provider);
These are two of the overloads of the method.
using System.Globalization;
DateTime dt1 = Convert.ToDateTime("2026-10-16 18:30:00");
Console.WriteLine(dt1.ToString("yyyy-MM-dd HH:mm"));
var de = CultureInfo.GetCultureInfo("de-DE");
DateTime dt2 = Convert.ToDateTime("16.10.2026", de);
Console.WriteLine(dt2.ToString("yyyy-MM-dd"));
In the example, we convert two strings to DateTime values. The
ISO 8601 format is understood in all cultures. The second string uses the
German date format, so we pass the German culture. For exact formats, the
DateTime.ParseExact method is a better choice.
$ dotnet run 2026-10-16 18:30 2026-10-16
Source
In this article we have shown how to convert types in C# with the
Convert class.
Author
List all C# tutorials.