C# - Type Casting
The type casting is a method of converting the value of one data type to another data type. It is also known as type conversion. In C#, there are several kinds of conversions. However, in this tutorial we will focus only on two major types.
- Implicit Type Casting
- Explicit Type Casting
Implicit Type Casting
The implicit type casting happens automatically when converting a smaller data types to larger data types. The compiler implicitly typecast the smaller data types to the larger data types. No data will be lost in this process.
Example
The example below shows how the implicit type casting is done in C#.
using System; class MyProgram { static void Main(string[] args) { byte num_byte = 10; //implicit Casting short num_short = num_byte; int num_int = num_short; long num_long = num_int; float num_float = num_long; double num_double = num_float; //printing variables Console.WriteLine("num_byte = {0}", num_byte); Console.WriteLine("num_short = {0}", num_short); Console.WriteLine("num_int = {0}", num_int); Console.WriteLine("num_long = {0}", num_long); Console.WriteLine("num_float = {0}", num_float); Console.WriteLine("num_double = {0}", num_double); } }
The output of the above code will be:
num_byte = 10 num_short = 10 num_int = 10 num_long = 10 num_float = 10 num_double = 10
Explicit Type Casting
The explicit type casting does not happen automatically. It is performed manually by calling the compiler explicitly to typecast the larger data types into the smaller data types. There might be a data loss in this process.
Example
The example below shows how the perform explicit type casting by calling the compiler explicitly.
using System; class MyProgram { static void Main(string[] args) { double num_double = 10.5; //explicit Casting float num_float = (float) num_double; long num_long = (long) num_float; int num_int = (int) num_long; short num_short = (short) num_int; byte num_byte = (byte) num_short; //printing variables Console.WriteLine("num_double = {0}", num_double); Console.WriteLine("num_float = {0}", num_float); Console.WriteLine("num_long = {0}", num_long); Console.WriteLine("num_int = {0}", num_int); Console.WriteLine("num_short = {0}", num_short); Console.WriteLine("num_byte = {0}", num_byte); } }
The output of the above code will be:
num_double = 10.5 num_float = 10.5 num_long = 10 num_int = 10 num_short = 10 num_byte = 10
Type Conversion methods
In C#, there are several built-in methods which can be used for type conversion. These methods are mentioned below:
Methods | Description |
---|---|
ToBoolean | Converts a type to a Boolean value, if possible. |
ToByte | Converts a type to a byte type, if possible. |
ToChar | Converts a type to a single Unicode character, if possible. |
ToDateTime | Converts a type (integer or string) to a date-time value. |
ToDecimal | Converts a floating point or integer type to a decimal number. |
ToDouble | Converts a type to a double-precision floating-point number. |
ToInt16 | Converts a type to a 16-bit signed integer. |
ToInt32 | Converts a type to a 32-bit signed integer. |
ToInt64 | Converts a type to a 64-bit signed integer. |
ToSbyte | Converts a type to a signed byte type, if possible. |
ToSingle | Converts a type to a single-precision floating-point number. |
ToString | Converts a type to a string. |
ToType | Converts a type to a specified type. |
ToUInt16 | Converts a type to an unsigned 16-bit unsigned integer. |
ToUInt32 | Converts a type to an unsigned 32-bit unsigned integer. |
ToUInt64 | Converts a type to an unsigned 64-bit unsigned integer. |
Example
The example below shows how the perform narrowing conversion by calling the compiler explicitly.
using System; class MyProgram { static void Main(string[] args) { bool x = true; float y = 50.55f; //using type conversion methods Console.WriteLine(x.ToString()); Console.WriteLine(y.ToString()); //another way of using methods Console.WriteLine(); Console.WriteLine(Convert.ToString(x)); Console.WriteLine(Convert.ToString(y)); } }
The output of the above code will be:
True 50.55 True 50.55