Conversion Operators
Conversion operators facilitate the implicit or explicit casting of user-defined types to built-in types or even other user-defined types. Developers implement a conversion operator so the compiler will know how to convert a user-defined type to another type. Like mathematical and logical operators, the primary reason for implementing conversion operators is convenience. It is never required. You could as easily expose ToType methods, such as ToInt, ToFloat, or ToDecimal. You are already familiar with ToString, which converts from any type to a string.
An implicit cast is available where truncation will not result (a safe cast), whereas explicit casting is required when truncation might occur. For primitive types, a safe cast ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access