为什么System.Timers.Timer.Interval双的数据类型?数据类型、Timers、System、Interval

2023-09-03 01:22:11 作者:没钱万万不能

这是一个有点学术问题,因为我与微软背后的思想采用双作为Interval属性的数据类型奋斗!

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!

首先从MDSN间隔的时间,以毫秒为单位,经过的事件之间;我会除preT这是一个离散的数字,为什么使用了双?当然int或长,使更多的意义!?

Firstly from MDSN Interval is the time, in milliseconds, between Elapsed events; I would interpret that to be a discrete number so why the use of a double? surely int or long makes greater sense!?

能否区间支持率就像5.768585(5.768585毫秒)?尤其是当一个人认为System.Timers.Timer的无处有接近亚毫秒级精确度...... 最精确的计时器在.NET?

Can Interval support values like 5.768585 (5.768585 ms)? Especially when one considers System.Timers.Timer to have nowhere near sub millisecond accuracy... Most accurate timer in .NET?

似乎有点愚蠢的我..也许我失去了一些东西!

Seems a bit daft to me.. Maybe I'm missing something!

推荐答案

拆卸表明,区间通过调用消耗(INT)Math.Ceiling(this.interval)所以,即使你指定一个实数,它会在使用前变成了 INT 。这发生在一个名为方法 UpdateTimer

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.

为什么呢?不知道,也许是规范说,被要求在一个点上,并且改变了吗?最终的结果是,要求不严格,因为它最终会转化为 INT ,不能大于 Int32.MaxValue 根据文档反正。

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.

是的,定时器能支持实数,它只是不告诉你,它静静地改变了他们。您可以初始化并运行 100.5d 计时器,它把它变成 101

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.

是的,这是所有有点愚蠢:4浪费字节,潜在的隐式转换,转换的呼叫,明确铸件,如果他们只希望使用的所有不必要的 INT

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.