- Implicit Conversion
- Explicit Conversion
Implicit Conversion
Implicit Conversion is automatically done by the compiler. For implicit conversion, we don't have to do anything manually. During implicit conversion, there is no loss of information and there is no possibility of throwing any overflow exception when the conversion is done.
Example
- class Program {
- static void Main(string[] args) {
- int i = 100;
- float f = i; // Implicit Conversion.
- Console.WriteLine("Converted float value is :{0}", f);
- }
- }
In the above line of code, an integer is implicitly converted into float. There won't be any loss of data because float is much bigger datatype compared to an integer and also, it won't throw any overflow exception.
Explicit Conversion
Explicit conversion is the conversion where we need to manually convert from one datatype to another datatype. Compiler won't do the conversion for us.
For Example, when we convert a float datatype to int datatype, we lose the fractional part (loss of information) and also, there is a possibility of throwing an overflow exception.
This is the main reason compiler won't do the conversion for us and we are forced to do the conversion explicitly.
There are 2 ways we can explicitly convert one datatype to another datatype.
- Type Cast operator
- Convert class
Type Cast Operator - (int)
Type Cast operator can be used for explicitly converting one datatype to another datatype. When we use a Type Cast operator for converting one datatype to another datatype we won't get the exception if the number which is used for conversion is much bigger than the expected range. It will only give a random negative value (-23452344).
Convert Class - Convert.ToInt32()
Convert Class also can be used for explicitly converting one datatype to another datatype. When we use a Convert class for converting one datatype to another datatype, compiler will generate an unhandled exception if the value which is used for converting is outside the expected range.
Example

Vaibhav DeshmukhPosted Mar 21, 2017, 2:05 AM
Very neat and clear. Thanks for sharing.