Read the syntax
An implicit conversion happens when C# permits a value to fit a target type without a cast; an explicit cast uses (TargetType) and may discard information. Conversion and parsing are different from changing a variable’s declared type.
Parse expects valid text and throws for invalid input. TryParse reports success with a bool and writes the converted value to an out variable; use an explicit culture when parsing decimal punctuation from fixed-format data.
Syntax
(targetType) valueExamples
Implicit and Explicit Casting
Automatic widening versus manual narrowing conversions.
int num = 100;
double converted = num; // implicit: int -> double, safe
Console.WriteLine(converted); // 100
double price = 19.99;
int wholePrice = (int)price; // explicit cast required
Console.WriteLine(wholePrice); // 19 (truncated, not rounded)- Automatic widening versus manual narrowing conversions.
- An implicit conversion happens when C# permits a value to fit a target type without a cast; an explicit cast uses (TargetType) and may discard information. Conversion and parsing are different from changing a variable’s declared type.
- Assigning int num to double converted needs no cast and prints 100. Casting 19.99 to int discards its fractional part, so wholePrice prints 19 rather than rounding to 20.
Parsing Strings to Numbers
Parse() converts text to a number, throwing an exception if the text isn't valid.
using System.Globalization;
string numberText = "42";
int parsed = int.Parse(numberText);
double parsedDouble = double.Parse("3.14", CultureInfo.InvariantCulture);
Console.WriteLine(parsed + 8); // 50
Console.WriteLine(parsedDouble * 2); // 6.28- Parse() converts text to a number, throwing an exception if the text isn't valid.
- An implicit conversion happens when C# permits a value to fit a target type without a cast; an explicit cast uses (TargetType) and may discard information. Conversion and parsing are different from changing a variable’s declared type.
- Parsing 42 produces an int, so adding 8 prints 50. Parsing 3.14 with invariant culture produces a double, and multiplying by 2 prints 6.28 under a dot-decimal culture.
Safe Parsing with TryParse
TryParse() avoids an exception, instead returning a bool indicating success.
string input = "abc";
if (int.TryParse(input, out int result))
{
Console.WriteLine($"Parsed: {result}");
}
else
{
Console.WriteLine($"'{input}' is not a valid number");
}- TryParse() avoids an exception, instead returning a bool indicating success.
- An implicit conversion happens when C# permits a value to fit a target type without a cast; an explicit cast uses (TargetType) and may discard information. Conversion and parsing are different from changing a variable’s declared type.
- The text abc is not a valid integer, so TryParse returns false and the else branch explains the failure. No FormatException is needed for this expected user-input mistake.
The Convert Class
Convert provides a general-purpose way to convert between many types, handling null gracefully (converts it to a default value).
using System.Globalization;
string text = "123";
int number = Convert.ToInt32(text);
Console.WriteLine(number + 1); // 124
double d = Convert.ToDouble("3.14", CultureInfo.InvariantCulture);
string backToString = Convert.ToString(number);
Console.WriteLine(backToString + " units");- Convert provides a general-purpose way to convert between many types, handling null gracefully (converts it to a default value).
- An implicit conversion happens when C# permits a value to fit a target type without a cast; an explicit cast uses (TargetType) and may discard information. Conversion and parsing are different from changing a variable’s declared type.
- Convert.ToInt32 turns 123 into an int so adding one prints 124. Convert.ToString turns it back into text, which can be concatenated with units; decimal display depends on culture.
Best practices
- Use TryParse() instead of Parse() whenever the input might not be valid, to avoid a FormatException crashing your program
- Use explicit casting only when you understand and accept the potential loss of precision from a narrowing conversion
- Prefer int.Parse()/TryParse() over Convert.ToInt32() when you specifically expect a string - Convert is more general-purpose but less precise about intent
- Remember casting a double to an int truncates the decimal part rather than rounding - use Math.Round() first if you want proper rounding
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference