Casting vs Converting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Casting vs Converting

How to explain in simple way the difference between casting and converting?

26th Jul 2018, 5:43 AM
Bartosz Kiliś
Bartosz Kiliś - avatar
1 Answer
+ 4
Bartosz Kiliś When you ask about casting vs converting, you are actually asking about: explicit(casting) conversion and implicit conversion. Keeping it simple I'll use int and decimal types. Let say we want to convert an int to a decimal: decimal total = 34, here we are using implicit conversion because there is no information lost converting an int to a decimal. Int 34 is now decimal 34.0. But if we want to convert a decimal to an int we would have to cast or do an explicit conversion basically letting the compiler know that we want to do this conversion and yes we understand there may some loss of information: int total = (int)34.5, in this case, int total = 34 and the .5 is loss. To gain a deeper understanding follow the link- https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions I hope this helps.
26th Jul 2018, 7:23 AM
ODLNT
ODLNT - avatar