I see
Fritz just posted
a code sample that makes use of C# conversion operators. I don't like it. No sir, I don't like it one bit. :)
Here's the deal. A conversion operator is just a way of writing a method that you invoke using the C# casting syntax. The thing is, since it's a method, why not just make it a method. If I were writing this code, I'd call it ToUpdateItem() or perhaps emphasize its a factory nature by calling it something like CreateFromUpdateItem.
There are two problems with the type conversion operator approach as I see it. First, you're providing something that implies type compatibility, when really no such type relationship exists. Second, the conversion operator syntax implies that no new instance is created, when in fact a new object instance is created every time.
With a plain ol' method, people who maintain your code can see exactly what's going on. With a type conversion operator, you're just twisting the language syntax to make it look like one thing is happening, when really something else is.
A good rule of thumb is that if you see yourself typing "public static operator", you're doing something wrong. :)
Posted
Dec 09 2005, 12:22 PM
by
craig-andera