views:

148

answers:

3

I was doing some type conversion routines last night for a system I am working on. One of the conversions involves turning string values into their DateTime equivalents.

While doing this, I noticed that the Convert.ToDateTime() method had an overload which accepted a boolean parameter.

First question? Under what circumstances could this ever be useful?

I went a little further and tried to execute the method in QuickWatch. Either way ( true or false ), the routine returns an InvalidCastException.

Second question? Why is this method even here?

EDIT

Thanks for the answers, guys. I can see how it makes sense from a contractual point of view, but it does seem odd that the core framework includes methods that:-

  • Can never work
  • Worse, will actually throw an exception when someone tries to call it.

It's a bit like someone making a car loaded with controls that actively stop your vehicle from working when used.

+5  A: 

It makes sense because ToDateTime is part of the IConvertible interface implemented by bool. If you look in reflector you will see that it throws an InvalidCastException.

Update (from Convert):

public static DateTime ToDateTime(bool value)
{
    return ((IConvertible) value).ToDateTime(null);
}
klausbyskov
But: this is a static method which can't be part of an interface. So this isn't logical at all.
Stefan Steinegger
@Stefan Steinegger see my update.
klausbyskov
Still: there is no use to implement a static method which shouldn't be called. The static method is statically bound by the compiler, there is nothing dynamic here and there is no use for this method.
Stefan Steinegger
@Stefan Steinegger I think it makes sense. Convert contains convenience method to call all conversions on all types that implement `IConvertible`. From an API design point of view I think it would be weirder (or at least equally weird) if it had been left out.
klausbyskov
It just turns a compile time error into a runtime error.
Stefan Steinegger
+3  A: 

If you look closely, most of the overloads are invalid and will throw an InvalidCastException.

It has to implement all of the casts as it implements IConvertible and this is the only way to do it correctly.

Oded
These are static methods, they do not implement any interfaces. So the question remains: why are these methods there?
Stefan Steinegger
+4  A: 

I think it's there for completeness and that you get an explicit InvalidCastException when calling Convert.ToDateTime with an object that is a bool.

If you look at all the members of Convert, you can see that overloads have been included to accept all the basic types for conversion to each of the other basic types, with InvalidCastExceptions being thrown when there is no sensible conversion.

I guess they thought this would be more meaningful than just not having the overloads there at all.

DanK
I think, this is the only explanation for this. The *thought" it would be better to make it complete. But actually, there is no use of it.
Stefan Steinegger