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)Parsing Strings to Numbers
Parse() converts text to a number, throwing an exception if the text isn't valid.
string numberText = "42";
int parsed = int.Parse(numberText);
double parsedDouble = double.Parse("3.14");
Console.WriteLine(parsed + 8); // 50
Console.WriteLine(parsedDouble * 2); // 6.28Safe 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");
}The Convert Class
Convert provides a general-purpose way to convert between many types, handling null gracefully (converts it to a default value).
string text = "123";
int number = Convert.ToInt32(text);
Console.WriteLine(number + 1); // 124
double d = Convert.ToDouble("3.14");
string backToString = Convert.ToString(number);
Console.WriteLine(backToString + " units");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
At a glance
- Purpose
- Applications on the .NET platform
- File extension
- .cs
- Runs in
- .NET runtime
- Usually used with
- .NET SDK and libraries
Specifications & further reading
Related C# documentation
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.