2.17. Conversion Operators
C# provides a mechanism by which each class can define a set of both implicit and explicit conversions that can be applied to objects of its class type. Why might we wish to do that?
Imagine that we have designed a BitVector class. As a convenience to our users, we choose to convert its internal sequence of 0,1 bits into either an unsigned long or a string representation. Similarly, we may wish to allow a string or unsigned long to be used wherever a BitVector is expected and have the program know how to convert those representations into a BitVector.
In one direction, we provide one or more conversion algorithms to change an object of the class type into a different type. Typically, these operators are specified as ...
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