I recently encountered some weird behaviour in the .NET TimeSpan
implementation.
TimeSpan test = TimeSpan.FromMilliseconds(0.5);
double ms = test.TotalMilliseconds; // Returns 0
FromMilliseconds
takes a double as parameter. However, it seems the value is rounded internally.
If I instantiate a new TimeSpan
with 5000 ticks (.5 ms), the value of TotalMilliseconds
is correct.
Looking at the TimeSpan
implementation in reflector reveals that the input is in fact casted to a long.
Why did Microsoft design the FromMilliseconds
method to take a double a parameter instead of a long (since a double value is useless given this implementation)?