C# code works with many types, both those provided by the language and many you will create yourself. This chapter explains how types may be related to each other while exploring the concepts of implicit and explicit type casting and obtaining the type of an object.
As previously outlined, C# is what is known as a strongly typed language. This means that once a variable has been declared as a particular type, it cannot subsequently be changed to a different type. This also restricts the type of value that can be assigned to the variable. It is not, for example, possible to assign a string
value to an int
variable.
Under some circumstances, however, assigning a value of one type to a variable of another type is possible. This involves the use of implicit and explicit casting.
Implicit casting
In instances where it is safe to do so without data loss, C# will allow you to assign a value from one type of variable to another simply using the assignment operator. For example, since a long
variable is quite capable of storing any value that can be stored in an int
variable, an assignment such as the following is perfectly valid:
int myInteger = 20; long myLong; myLong = myInteger;
This is referred to as implicit casting since the casting from int
to long
is implied by the type of the value, as opposed to having been explicitly declared within the code.
There are, however, limits to implicit casting. It is not possible, for example, to assign a long
to an int
using implicit casting because a long
is capable of storing significantly larger numbers than an int (an int
is 4 bytes in length versus 8 bytes for a long
). Attempting to squeeze a long
into an int
variable would inevitably result in lost data. For this reason, the C# compiler will flag such an attempt as an error, and the code will fail to compile, as demonstrated below:
long myLong = 1381292100921; int myInteger; myInteger = myLong;
When the compiler encounters the above code, it will declare the following syntax error:
error CS0266: Cannot implicitly convert type `long' to `int'. An explicit conversion exists (are you missing a cast?)
Code language: JavaScript (javascript)
As the error message indicates, it may be possible to perform this operation using an explicit cast.
Explicit casting
While the compiler may prevent an implicit cast, it is possible that you, as the programmer, know that even though a variable is a long
type, it will never contain a value greater than an int
variable can store. In this case, you might legitimately want to assign the value stored in a long
variable to an int
variable. This can be achieved by using an explicit cast to convert the long
value to an int
.
Explicit casts are performed by placing the variable type to which you wish to convert in parentheses (()
) before the name of the variable you want to convert from. The following code, for example, casts a long
value to an int
during an assignment operation:
long myLong = 138129210; int myInteger; myInteger = (int) myLong;
Care should be taken when using explicit casts. If the value being cast is greater than the storage capacity of the destination variable, the value will change without warning. For example, consider the following modification of the above example:
long myLong = 138129210212; int myInteger; myInteger = (int) myLong; System.Console.WriteLine($"myLong = {myLong}"); System.Console.WriteLine($"myInteger = {myInteger}");
The output from the above code illustrates that the value assigned to myInt
no longer matches that assigned initially to myLong
:
myLong = 138129210212
myInteger = 690256740
In this case, the myLong
value exceeds the int
data type’s storage capacity, causing the variable’s value to change without warning. In a complex development project, a bug of this nature may be difficult to locate.
Similarly, assigning a floating-point value to a variable that only stores whole numbers will result in the loss of the fractional part of the original value.
Note: Casting is only possible on numerical data types. It is not, therefore, possible to perform casts on string
, bool
or char
data types.
Identifying the type of a variable
Identifying a variable’s type in C# is also possible by calling the variable’s GetType()
method. The following code, for example, outputs the types of the myVar1
and myVar2
variables:
string myVar1 = "Hello"; double myVar2 = 123123.12; System.Console.WriteLine(myVar1.GetType()); System.Console.WriteLine(myVar2.GetType());
Output:
System.String
System.Double
Code language: CSS (css)