This is a short post about the not so well known implicit and explict operators in C#. These operators are used for user-defined conversions of custom types, and their existence is an often overlooked feature of C# (at least overlooked by me, until recently).
An implicit cast occurs automatically when an implicit conversion operator has been defined and the two data types are compatible. An explicit cast is used when the two data types are not entirely compatible and requires a cast.
For example, after we defined an implict cast from List to DmCollection like this:
return new DmCollection<T>(list);
we can now cast a List to a DmCollection:
DmCollection<T> col = (DmCollection<T>) list;
An example of an implicit operator for the conversion of a double to a Vector class:
we can now assign a Vector to a variable of type double without the need for an explicit cast:
double d = v;