Casting and converting between types
You will often need to convert values of variables between different types. For example, data input is often entered as text at the console, so it is initially stored in a variable of the string
type, but it then needs to be converted into a date/time, or number, or some other data type, depending on how it should be stored and processed.
Sometimes you will need to convert between number types, like between an integer and a floating-point, before performing calculations.
Converting is also known as casting, and it has two varieties: implicit and explicit. Implicit casting happens automatically, and it is safe, meaning that you will not lose any information.
Explicit casting must be performed manually because it may lose information, for example, the precision of a number. By explicitly casting, you are telling the C# compiler that you understand and accept the risk.
Casting numbers implicitly and explicitly
Implicitly casting...