Convert.ToDateTime(D) compared to (DateTime)D

后端 未结 2 1903
抹茶落季
抹茶落季 2021-01-24 17:11

If I have a value D and want to ensure it is type datetime then is there a difference between the following?

  1. DateTime dtm = Convert.ToDateTi
2条回答
  •  隐瞒了意图╮
    2021-01-24 18:08

    Convert.ToDateTime has several overloads that will convert other types to DateTime. It should be used when you're converting an instance that isn't a DateTime to a DateTime instance. You could also use DateTime.Parse and DateTime.TryParse (if you're trying to parse a string representation).

    (DateTime)D attempts to directly cast an instance to DateTime. If the instance isn't already a DateTime prior to the call, an exception will be thrown.

提交回复
热议问题