Craig had a
rather strong reaction to
my post about type conversion operators in C#, and after some back and forth in the comments to his post, I'm now convinced that it's probably better to avoid them altogether.
While it is convenient to be able to just cast one object to another, the problem is that it really messes with the concept of identity.
Bertrand Le Roy expressed a similar concern to Craig's in a comment to my post, and put it quite succinctly:
"IMHO it all boils down to (someObject == (SomeType)someObject) being false with your code, which looks very wrong. "
Because the type conversion operator allocates a new object of the desired type, this expression will return false, and I agree that this just feels wrong, and can certainly lead to some unexpected behavior if you're not careful.
So I'm glad I posted the previous entry, since the response has caused me to re-evaluate the code I was writing and made me re-implement the type conversion operators as simple "ConvertTo..." methods. Almost as terse, just as intuitive, and no messing with identity.
Posted
Dec 12 2005, 05:40 PM
by
fritz-onion