Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
412 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...