O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required