Casting and Converting Between Types
Learn about casting for implicit and explicit conversions, rounding rules, and using System.Convert, parsing from strings to other data types.
We will often need to convert the values of variables between different types. For example, data input is usually entered as text at the console, initially stored in a string
type variable. Still, depending on how it should be stored and processed, it must be converted into a date/time, number, or other data type. Sometimes, we must convert between number types, like an integer and a floating point, before calculating.
Casting
Converting, also known as casting, has two varieties: implicit and explicit.
Implicit casting: Happens automatically and is safe, meaning we will not lose any information.
Explicit casting: This must be performed manually because it may lose information, such as a number's precision. By explicitly casting, we tell the C# compiler we understand and accept the risk.
Casting numbers implicitly and explicitly
Implicitly casting an int
variable into a double
variable is safe because no information can be lost, as the following shows:
Step 1: Use your preferred coding tool to add a new "Console App or console project" named CastingConverting
to the Chapter03
workspace/solution
.
In Visual Studio Code, select
CastingConverting
as the activeOmniSharp
project.
Step 2: In the Program.cs
, delete the existing statements and then type statements to declare and assign an int
variable and a double
variable, and then implicitly cast the integer’s value when assigning it to the double
variable, as shown in the following code:
int a = 10;double b = a; // an int can be safely cast into a doubleWriteLine(b);
Step 3: Type statements to declare and assign a double
variable and an int
variable, and then implicitly cast the double
value when assigning it to the int
variable, as shown in the following code:
double c = 9.8;int d = c;WriteLine(d); // d is 9 losing the .8 part
Step 4: Run the code and note the error message, as shown in the following output:
Error: (6,9): error CS0266: Cannot implicitly convert type 'double' to'int'. An explicit conversion exists (are you missing a cast?)
This error message will also appear in the Visual Studio error list or Visual Studio Code problems window. We cannot implicitly cast a double
variable into an int
variable because it is potentially unsafe and could lose data, like the value after the decimal point. We must explicitly cast a double
variable into an int
variable using a pair of round brackets around the type into which we want to cast the double
type.
The pair of round brackets is the cast operator. Even then, we must beware that the part after the decimal point will be trimmed off without warning because we have chosen to perform an explicit cast and understand the consequences.
Step 5: Modify the assignment statement for the d
variable, as shown in the following code:
int d = (int)c;WriteLine(d); // d is 9 losing the .8 part
Step 6: Run the code to view the results, as shown in the following output:
109
When converting values between larger and smaller integers, we must perform a similar operation. Again, beware that we might lose information because any value too big will have its bits copied and interpreted in ways we might not expect.
Step 7: Enter statements to declare and assign a long
64-bit variable to an int
32-bit variable, both using a small value that will ...