Implicit and Explicit Casting
Implicit and Explicit Casting
When working with data types,sometimes we have to convert between data types.This can be lead to
unexpected errors sometimes.To avoid that,languages like C# has casting.
Implicit Casting
Imagine you are trying to put water in a small glass to a larger glass with more capacity.There is no
doubt that this is possible without any issue.It is safe to do that.
Similary,what was in a variable of a smaller data type can be assigned to a larger capacity data type
without any problem.You don’t need any special statement or methods for this.
(But remember when it comes to data types.is it not just the capacity we have to consider.We have to
consider the accessibility as well)
Explicit Casting
Imagine you are trying to empty a larger glass of milk to a smaller glass of milk.Sometimes you may be
able to do it without spilling.But if the larger glass was full to it’s capacity,definitely milk will
overflow.When you are trying to put a value in a larger data type to a smaller capacity data type,you will
experience the same.
To avoid this,in C#.NET (and in many other languages) you have the explicit casting option .
It is pretty much like you are telling the program that you need to deliberately do this and you know the
possible risk of the conversion.