Why is the data type of System.Timers.Timer.Interval a double?

后端 未结 2 708
梦谈多话
梦谈多话 2020-12-11 01:29

This is a bit of an academic question as I\'m struggling with the thinking behind Microsoft using double as the data type for the Interval property!

Firstly from MDS

2条回答
  •  悲哀的现实
    2020-12-11 01:49

    Disassembling shows that the interval is consumed via a call to (int)Math.Ceiling(this.interval) so even if you were to specify a real number, it would be turned into an int before use. This happens in a method called UpdateTimer.

    Why? No idea, perhaps the spec said that double was required at one point and that changed? The end result is that double is not strictly required, because it is eventually converted to an int and cannot be larger than Int32.MaxValue according to the docs anyway.

    Yes, the timer can "support" real numbers, it just doesn't tell you that it silently changed them. You can initialise and run the timer with 100.5d, it turns it into 101.

    And yes, it is all a bit daft: 4 wasted bytes, potential implicit casting, conversion calls, explicit casting, all needless if they'd just used int.

提交回复
热议问题