Explain the implicit and explicit conversion of data types with examples?( .NET interview questions on data type)
- By Shiv Prasad Koirala in .Net
- Dec 21st, 2013
- 9712
- 0
To understand implicit and explicit conversion of data types first let's try to understand type casting concept. Type casting is a mechanism where we convert one type of data to other type.
For example below is simple code where we want to move integer (value without decimals) value to a double ( value with decimals). When we try to move double data type to integer data type casting occurs. Casting is also termed as conversion.
int i = 100;
double d = 0;
d = i; // casting
In the above example there is no loss of data. In other words when we moved 100 integer value to double we get the 100 value as it. This is termed as implicit conversion / casting.
Now consider the below code where we are trying to move a double to an integer. In this scenario in integer only 100 will be received decimals will be removed. You can also see I need to explicitly specify that we need to convert in to integer. This is termed as explicit conversion.
double d = 100.23;
int i = 0;
i = (int) d; // this will have 100, 0.23 will truncated.
This question is taken from the famous .NET interview question book by Bpb publication read more about the book from here http://www.flipkart.com/net-interview-questions-6th/p/itmdyuqzdqx8cvqx
Below is a nice video created by www.questpond.com which demonstrates type safety , casting , implicit casting and explicit casting.
Shiv Prasad Koirala
Visit us @ www.questpond.com or call us at 022-66752917... read more