C# | Type Casting - GeeksforGeeks

19 Jan, 2019
Type conversion happens when we assign the value of one data type to another. If the data types are compatible, then C# does Automatic Type Conversion. If not comparable, then they need to be converted explicitly which is known as Explicit Type conversion. For example, assigning an int value to a long variable.
Implicit Type Casting / Automatic Type Conversion
It happens when:
The two data types are compatible.
When we assign value of a smaller data type to a bigger data type.
For Example, in C#, the numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other. Before converting, the compiler first checks the compatibility according to the following figure and then it decides whether it is alright or there some error.
Following table shows the implicit types of conversion that is supported by C# :
Example :
// C# program to demonstrate the// Implicit Type Conversionusing System;namespace Casting{  class GFG {          // Main Method        public static void Main(String []args)        {            int i = 57;                           // automatic type conversion            long l = i;                            // automatic type conversion            float f = l;                          // Display Result            Console.WriteLine("Int value "  +i);            Console.WriteLine("Long value "  +l);            Console.WriteLine("Float value "  +f);        }}}
Int value 57
Long value 57
Float value 57

Explicit Type Casting
There may be compilation error when types not compatible with each other. For example, assigning double value to int data type:
// C# program to illustrate incompatible data // type for explicit type conversionusing System;namespace Casting{  class GFG {          // Main Method        public static void Main(String []args)        {            double d = 765.12;              // Incompatible Data Type            int i = d;                          // Display Result                Console.WriteLine("Value of i is ", +i);        }}}
Error :
prog.cs(14,21): error CS0266: Cannot implicitly convert type `double' to `int'.
An explicit conversion exists (are you missing a cast?)

So, if we want to assign a value of larger data type to a smaller data type we perform explicit type casting.
This is useful for incompatible data types where automatic conversion cannot be done.
Here, target-type specifies the desired type to convert the specified value to.
Sometimes, it may result into the lossy conversion.
Example :
// C# program to demonstrate the// Explicit Type Conversionusing System;namespace Casting{  class GFG {          // Main Method        public static void Main(String []args)        {            double d = 765.12;              // Explicit Type Casting            int i = (int)d;              // Display Result            Console.WriteLine("Value of i is " +i);        }}}
Value of i is 765

Explanation :Here due to lossy conversion, the value of i becomes 765 and there is a loss of 0.12 value.
Example :
// C# program to demonstrate the// Built- In Type Conversion Methodsusing System;namespace Casting{  class GFG {          // Main Method        public static void Main(String []args)        {            int i = 12;            double d = 765.12;            float f = 56.123F;                          // Using Built- In Type Conversion            // Methods & Displaying Result            Console.WriteLine(Convert.ToString(f));            Console.WriteLine(Convert.ToInt32(d));            Console.WriteLine(Convert.ToUInt32(f));            Console.WriteLine(Convert.ToDouble(i));            Console.WriteLine("GeeksforGeeks");        }}}
56.123
765
56
12
GeeksforGeeks

CSharp-Basics
CSharp-data-types
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# Dictionary with examples
Introduction to .NET Framework
Difference between Ref and Out keywords in C#
C# | String.IndexOf( ) Method | Set - 1
C# | Abstract Classes
Extension Method in C#
C# | Delegates
HashSet in C# with Examples
Different ways to sort an array in descending order in C#
C# | Arrays