Answer: Casting a data type is nothing but converting a variable from one data type to another data type. This can also be called as data type conversion.
There are two types of data type conversions as given below.
Implicit conversions: Here the conversion is type safe i.e. no special syntax is required for this. With this you can convert from derived classes to base classes. In this conversion, there is no chance of data loss.
Explicit conversions: Here the conversion requires a cast operator. Here both source and destination variables are compatible, but might be risk in data loss because of the type or size of destination variable is smaller than the source variable.
Example:
class clsBase=new class();
method m=clsBase; //Implicit conversion
class clsDerive=(class) m; //Explicit conversion
Asked In: Many Interviews |
Alert Moderator