The SLAR on System.Convert [Brad Abrams]

It is been a while sense I posted in the series sharing some of the information in the .NET Framework Standard Library Annotated Reference Vol 1. Given today is BCL blog day, I thought I would kick it off by adding a couple of annotations from the System.Convert class. Enjoy!

Brad Abrams:

Early in the design of the BCL we went back and forth trying to decide how we wanted conversions to be supported in the library. Initially we wanted to distribute

them as instance methods in each class:

int i = 10;

double d = Int32.ToDouble(i);

This design has the advantage of being more easily discoverable as it does not require an intermediate object. Later, we tried a different design that had a single object with all the conversions, so that the same conversion would be:

int i = 10;

double d = Convert.ToDouble(i);

We liked this approach because if the developer needs to change the type of the i variable to a long she would only have to change the declaration, not every usage:

long i = 10;

double d = Convert.ToDouble(i);

Anders Hejlsberg:

In the early days of .NET we had two types that could be used to represent dynamically typed values: System.Object and System.Variant. System.Variant was basically a managed wrapper for OLE Automation variants. Having two ways of doing what amounts to the same thing caused tremendous confusion in the libraries. Some APIs would use Object, others would use Variant.

Some even tried to support both, which introduced lots of ambiguities. We finally made the hard call to get rid of Variant. In retrospect, this was clearly the right decision. We can now say that “every piece of data in .NET is an object,” which brings about tremendous conceptual simplification. The types of operations that were traditionally provided by OLE Automation’s variant support, such VarChangeType() that converts a variant from one type to

another, is now provided by the Convert class and the IConvertible interface.