Implicit Casting vs. Explicit Casting in C-sharp
Type conversion:
- After a variable is declared, it cannot be declared again or assigned a value of another type unless that type is implicitly convertible to the variable's type.
Example:
bool result;
result = “Hello”;
Output: Cannot implicitly convert type 'string' to 'bool'
- To copy a value into a variable or method parameter of another type or to assign a class variable to a variable of an interface type, these kinds of operations are called Type Conversions.
Type Casting: In C#, there are two types of casting:
- Implicit Casting
- Explicit Casting
Implicit Casting
Implicit type casting means conversion of data types without losing its original meaning.
In Implicit Casting, no special syntax is required because the conversion always succeeds and no data will be lost, this is the safest type of casting.
Example:
// C# program to demonstrate the Implicit Type Conversion
using System;
namespace Casting
{
class Program
{
// 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);
}
}
}
Output:
Int value 57
Long value 57
Float value 57
During conversion, strict rules for type conversion are applied. If the operands are of two different data types, then an operand having lower data type is automatically converted into a higher data type.
This type of typecasting is essential when you want to change data types without changing the significance of the values stored inside the variable.
Implicit type conversion happens automatically when a value is copied to its compatible data type.
Implicit type conversion is also called standard type conversion. Converting from a smaller data type into larger data type is also called a type promotion.
Example:
char -> int -> long -> float -> double
We cannot perform implicit type casting on the data types which are not compatible with each other such as:
- Converting float to an int will truncate the fraction part hence losing the meaning of the value.
- Converting double to float will round up the digits.
- Converting long int to int will cause dropping of excess high order bits.
Explicit Casting
If a conversion cannot be made without a risk of losing information, the compiler requires that you perform an explicit conversion, which is called a cast. A cast is a way of explicitly informing the compiler that you intend to make the conversion and that you are aware that data loss might occur, or the cast may fail at runtime.
-
To force the type conversion in such situations, we use explicit type casting. It requires a type casting operator. The general syntax for type casting operations is as follows:
(type-name) expression
To perform a cast, specify the type that you are casting to in parentheses in front of the value or variable to be converted.
Example:
The following program casts a double to an int. The program will not compile without the cast.
// C# program to demonstrate the Explicit Type Conversion
class Test
{
static void Main()
{
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;
Console.WriteLine("x:" + x);
Console.WriteLine("a:" + a);
}
}
Output:
x:1234.7
a:1234
Summary
- Typecasting is also called type conversion.
- It means converting one data type into another.
- Converting a smaller data type into a larger one is also called type promotion.
- 'C' provides an implicit and explicit way of type conversion.
- Implicit type conversion operates automatically when the compatible data type is found.
- Explicit type conversion requires a type casting operator.
- Implicit type casting means conversion of data types without losing its original meaning whereas Explicit conversion cannot be made without a risk of losing information.
Keep in mind the following rules for programming practice when dealing with different data type to prevent from data loss :
- Integers types should be converted to float.
- Float types should be converted to double.
- Character types should be converted to integer.
Top comments (0)