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 ...

Get Programming Microsoft® Visual C#® 2008: The Language 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.