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 in 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, 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.
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 an...