c-style casting: This will throw InvalidCastException.
Code: Select all
int myVal = 100;
object obj = myVal;
string str = (string) obj;
Code: Select all
int myVal = 100;
object obj = myVal;
string str = obj as string;
- If you are sure that source is also from same datatype like destination then you can use general casting(UnBoxing) which gives better performance since it is costly to send an exception.
- If you are NOT sure that whether source will have same datatype like destination or not, then you can use c-style casting which gives more flexibility.