C# Tutorial For Beginners: Casting Variables
Updated: Jan 18, 2022
A variable Casting process is when you assign a value of one data type to another type. In c#, there are two types of data casting:
Implicit Casting – The cast operation is done automatically when we convert a smaller type to a larger type (int ->double, long->float, etc.).
Explicit Casting – The cast operation is done manually in any case of converting a larger type to a smaller size type (long -> int, double ->float, etc.).
Let’s start with the simple part first. Below are some examples demonstrating how ‘implicit’ casting is done.
Explicit casting is done using a dedicated syntax that tells the compiler to approve the casting process, although it may resolve data loss.
Type Conversion Methods
In addition to the examples above, we can also use built-in methods for converting data types as follow: