Type Casting in C# is defined as if we assign any data type to another data type then called it as “Typecasting”. Any programming language lower data type values can automatically cast into upper data type values. In this situation there is no loss of data, whereas in the case of upper data type value into lower data type value there may chance of loss of data. Lower data type to upper data type can automatically be done by C# compiler but upper data type to lower data typecasting, we must need explicit typecasting. Which is known as “Explicit casting”.
Let’s take an example of long value into int value is an explicit typecasting.
Let’s consider a small analogy then you will understand very clearly, there are 2 water bottles, one is 1 liter and the other one is 2 liters. We can easily pour 1 liter of water into 2 liters of the water bottle without overflowing. Same way if we try to pour 2 liters of water bottle water into 1 liter then there may be a chance to overflowing of water if a 2-liter water bottle contains more than 1 liter of water. So, in this case, a 1-liter water bottle is the lower data type and 2-liter water bottles are an upper data type. Even there is a chance to overflowing water still we want to pour 2 liters of water into a 1-liter water bottle we can pour so that customers must accept to do so. Same way developers have clear cut ideas even we try to casting upper data type into lower data types there may be a loss of data, so he must accept it.
There are 2 types of casting in C#.
byte->short->int->long->float->double
Code:
Bigger_dataType variableName=smaller_dataType_Value;
bigger data type to smaller data type conversion is said to be “Explicit typecasting”. This is not automatically done by the C# compiler. There may be a loss of data. This must be done by the developer explicitly.
byte->short, int, long, float, double
short->int, long, float, double
int->long, float, double
long->float, double
float->double
Code:
Smaller_dataType variableName=(Smaller_dataType)Bigger_dataType_Value;
Below are some examples are mentioned:
Implicit type casting
Code:
//including System package in C# using System; //Creating class public class ImplicitTypeCasting { // main method for application to run public static void Main(String []args) { //variable declaration and initialization int intType = 200; // Implicit int to long casting long longType = intType; // Implicit long to float casting float floatType = longType; // Printing output of implicit conversion variables Console.WriteLine("INT value after Implicit conversion: " +intType); Console.WriteLine("LONG value after Implicit conversion:" +longType); Console.WriteLine("FLOAT value after Implicit conversion: " +floatType); } }
Output:
Trying to bigger type to lower type Implicit type casting
Code:
//including System package in C# using System; //Creating class public class ImplicitCastingBiggerToSmaller { // main method for application to run public static void Main(String []args) { //variable declaration and initialization int intType = 200; // Trying to convert int to byte Implicitly but there is compile time error byte byteType = intType; // Trying to convert int to short Implicitly but there is compile time error short shortType = intType; // Printing output of implicit conversion variables Console.WriteLine("INT value after Implicit conversion: " +intType); Console.WriteLine("BYTE value after Implicit conversion:" +byteType); Console.WriteLine("SHORT value after Implicit conversion: " +shortType); } }
Output:
Explicit type casting
Code:
//including System package in C# using System; //Creating class public class ExplicitCastingBiggerToSmaller { // main method for application to run public static void Main(String []args) { //variable declaration and initialization int intType = 9999999; int intType1=120; // Trying to convert int to byte explicitly byte byteType = (byte)intType; byte byteType1 = (byte)intType1; // Trying to convert int to short explicitly short shortType = (short)intType; short shortType1 = (short)intType1; // Printing output of explicit conversion variables //Given int range is not in byte and short range so there must be loss of data result into incorrect output Console.WriteLine("BYTE value after Explicit conversion: " +byteType); Console.WriteLine("SHORT value after Explicit conversion: " +shortType); Console.WriteLine("\n"); // Printing output of explicit conversion variables //Given int range is in byte and short range so there no data loss Console.WriteLine("BYTE value after Explicit conversion: " +byteType1); Console.WriteLine("SHORT value after Explicit conversion: " +shortType1); } }<strong> </strong>
Output:
Explicit type casting with predefined methods
Code:
//including System package in C# using System; //Creating class public class ExplicitCastingBiggerToSmaller { // main method for application to run public static void Main(String []args) { //variable declaration and initialization int intType = 9999999; double doubleType=122.23; float floatType=222.222f; // Printing output of explicit convertion variables //By using C# predefined method for type casting Console.WriteLine("INT to STRING type value with predefined method convertion: "+Convert.ToString(intType)); Console.WriteLine("DOUBLE to INT type value with predefined method convertion: "+Convert.ToInt32(doubleType)); Console.WriteLine("FLOAT to INT type value with predefined method convertion: "+Convert.ToUInt32(floatType)); } }
Output:
C# there are 2 types of type castings are there, 1st one is implicit type casting and the second one is explicit typecasting. Implicit type casting automatically done by the compiler but explicit type casting developer must perform because in this case there may be a chance to lose of data.
The above is the detailed content of Type Casting in C#. For more information, please follow other related articles on the PHP Chinese website!