Advertisement
Beginner C# Programming, Data Types in C#

What is the difference between an implicit conversion and an explicit conversion?

Short answer

An implicit conversion happens automatically without requiring any special syntax, since the compiler knows the conversion is always safe and will not lose data, such as converting an int to a long.

Save C# Programming mock interview
Advertisement
AModel answer
An implicit conversion happens automatically without requiring any special syntax, since the compiler knows the conversion is always safe and will not lose data, such as converting an int to a long. An explicit conversion, in contrast, requires you to manually write a cast using parentheses around the target type, because the compiler cannot guarantee the conversion will succeed without potential data loss, such as converting a long back down to an int, which requires you to explicitly acknowledge that risk.
long bigNumber = 100;      // implicit conversion from int to long
int smallNumber = (int)bigNumber; // explicit conversion from long to int
Real-world example
A financial calculation stores intermediate results as a long to avoid overflow issues implicitly during addition, but requires an explicit cast whenever the final result needs to be stored back into a smaller int based field.
Likely follow-ups
  • Which specific type conversions in C# are always implicit?
  • What happens at runtime if an explicit conversion cannot actually succeed?
Related topics