Updated: Dec 13, 2020
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 converting 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 some examples that will demonstrate how ‘implicit’ casting is done.
Explicit casting is done using a dedicated syntax which 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: