Is TimeSpan unnecessary?

后端 未结 9 1848
野的像风
野的像风 2020-12-15 06:33

EDIT 2009-Nov-04

OK, so it\'s been a little while since I first posted this question. It seems to me that many of the initial responders failed to r

相关标签:
9条回答
  • 2020-12-15 07:06

    its sugar not more or less....

    0 讨论(0)
  • 2020-12-15 07:08

    One reason is that splitting the types prevents a class of bugs where you think you have a relative time but really have an absolute time, and vice versa. For example, addition of two absolute times can be flagged as a compiler error if the two types are separate.

    Also, IntelliSense (and discovery for newbies) works better when the number of members is smaller-- by splitting methods between the two types, working with each gets easier.

    0 讨论(0)
  • 2020-12-15 07:16

    I think you could make the opposite argument that DateTime is redundant, and we should only have TimeSpan :)

    Seriously, all dates really are just time spans. They are all relative to some starting point. Technically, there is no "year zero" in the Christian calendar (since you can't really have a "zeroth year of our lord"), but if we assign 12:00 A.M. January 1, 0001 B.C. as the "zero point", then every date that comes after (or before) can be thought of as relative to that date. So, 12:00 A.M. on September 19, 2009 would have a TimeSpan of 734033 days.

    So, mathematically, DateTime and TimeSpan are redundant. But when we write code, we are attempting to communicate much more than just abstract mathematical constructs. Any given DateTime instance may in fact just be a time span relative to some arbitrary zero point, but to most people reading your code, it will imply a particular point on the calendar. Similarly, a TimeSpan implies the gap between two points on the calendar.

    In this case, Microsoft has chosen to be clear rather than parsimonious. I can't say I disagree with the decision.

    0 讨论(0)
提交回复
热议问题