Updated April 6, 2023
Introduction to Type Casting in C#
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.
Why explicit casting required bigger data types to smaller data types?
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.
Types of Casting in C#
There are 2 types of casting in C#.
- Explicit type casting
- Implicit type casting: the smaller data type to bigger data type conversion is said to be “Implicit typecasting”. This is automatically done by the C# compiler. There is no loss of data.
1. Implicit Type Casting
byte->short->int->long->float->double
Code:
Bigger_dataType variableName=smaller_dataType_Value;
2. Explicit Type Casting
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;
Methods for Type Conversion
- ToBoolean: It will convert a type to a Boolean value.
- ToChar: It will convert a type to a character value.
- ToByte: It will convert a value to a Byte value.
- ToDecimal: It will convert a value to Decimal point value.
- ToDouble: It will convert a type to double data type.
- ToInt16: It will convert a type to a 16-bit integer
- ToInt32: It will convert a type to a 32-bit integer
- ToInt64: It will convert a type to a 64-bit integer
- ToString: It will convert a given type to String
- ToUInt16: It will convert a type to an unsigned 16-bit integer
- ToUInt32: It will convert a type to an unsigned 32-bit integer
- ToUInt64: It will convert a type to an unsigned 64-bit integer
Examples To Implement Type Casting in C#
Below are some examples are mentioned:
Example #1
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:
Example #2
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:
Example #3
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);
}
}
Output:
Example #4
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:
Conclusion
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.
Recommended Articles
This is a guide to Type Casting in C#. Here we discuss an introduction to Type Casting in C#, 2 types with methods for type conversion and respective examples. You can also go through our other related articles to learn more –