User-Defined Implicit and Explicit Conversions

Most programs operate on data of a diverse set of types, ranging from the predefined simple types (int, double, and so on) over the derived predefined .NET types (in the Framework class library) to your own custom made types. If an operation adds two int values together and assigns the result to an int variable, the operation stays within the same type (if the result stays within the range of an int). Sometimes, however, we combine different types in the same operations with an accompanying need to convert data from one type to another. As discussed in Chapter 6, “Types, Part I: The Simple Types,” typical circumstances that require conversions are as follows:

  • A value of one type is assigned to a ...

Get C# Primer Plus 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.