There is a general rule that the result of casting an object to its own type is the original object. If a storage location of type BaseType
holds an instance of DerivedType
, casting from the original type to DerivedType
should tell the compiler to use DerivedType
's members, but shouldn't actually "do" anything to the object. If custom base-to-derived conversion operators were allowed, it would be necessary to either: (1) Have a cast-to-instance's own-type operation sometimes yield a new object and sometimes not, or (2) Have a derived type object which is stored in a base-type storage location behave substantially differently from a base-type or unrelated-type object, without clearly visible type-checking code that would make it do so. While there are times one might want to have a method which, given a base-type parameter, might return a new derived-type object or--if given an instance of the derived type, simply return it unmodified, it is generally better to have such a thing "look" like a method instead of a typecast.
BTW, there is one situation where a compiler could allow user-defined type conversions between a "base type" and a "derived type" without the above ambiguity: when one of the types is a struct. Although C# pretends that value types inherit from ValueType
, every value type definition really defines two things: a heap object type which derives from ValueType
, and a collection of storage locations, which isn't an object and doesn't derive from anything. C# defines an implicit casting operator from the latter type to the former, and an explicit casting operator from the former to the latter. Since conversion between a heap object and a collection of storage locations is never reference preserving, allowing user-defined conversion operators to be used in such context would not cause muddled inheritance semantics. The only difficulty with such conversion would be the fact that value types which used them would either be unusable as generic types, or would lose their special behavior if passed as generic types.