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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.