Implicit and Explicit casting

Explicit Cast

In C#, you cast using the cast operator. By specifying the type you would like the variable converted to within parentheses, you acknowledge that if an explicit cast is occurring, there may be a loss of precision and data, or an exception may result

Explicit Casting:

long longNumber = 509876543456;

int intNumber = (int) longNumber;

In other instances, such as when going from an int type to a long type, there is no loss of precision, and no fundamental change in the value of the type occurs. In these cases, the code needs to specify only the assignment operator; the conversion is implicit. In other words, the compiler is able to determine that such a conversion will work correctly

//  Not Using the Cast Operator for an Implicit Conversion 
int intNumber = 31416;
long longNumber = intNumber;

Leave a comment