Basic Conversions

Conversions allow moving the value of one variable into another. There are two types of conversions: explicit and implicit. Implicit conversions happen without intervention. Table 2.7 shows the legal implicit conversions for C# simple types. In addition, the integer literal 0 (zero) can be implicitly converted to an enum. There are no implicit conversions to the char type.

Table 2.7. Legal Implicit Conversions for Simple C# Types
TypeAllowable Conversions
boolnone
charushort, int, uint, long, ulong, float, double, decimal
sbyteshort, int, long, float, double, decimal
byteshort, ushort, int, uint, long, ulong, float, double, decimal
shortint, long, float, double, decimal
Ushortint, uint, long, ulong, float, double, decimal
Intlong, ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.