Is there a difference in the returned values between Timespan(0,0,secs)
and Timespan.FromSeconds(secs)
?
It seems to me the difference is that FromSeconds accepts a double
.
Ultimately no, under the hood, TimeSpan
deals with ticks.
Personally I would prefer to use TimeSpan.FromSeconds
as it is completely clear what the intent is.
The parameter being a double
in the second case is an important difference indeed: in some cases it can lead to an OverflowException
. Quoting the documentation below.
TimeSpan Constructor (Int32, Int32, Int32):
The specified hours, minutes, and seconds are converted to ticks, and that value initializes this instance.
TimeSpan.FromSeconds Method:
The value parameter is converted to milliseconds, which is converted to ticks, and that number of ticks is used to intialize the new TimeSpan. Therefore, value will only be considered accurate to the nearest millisecond. Note that, because of the loss of precision of the Double data type, this can generate an OverflowException for values that are near but still in the range of either MinValue or MaxValue, This is the cause of an OverflowException, for example, in the following attempt to instantiate a TimeSpan object.
// The following throws an OverflowException at runtime TimeSpan maxSpan = TimeSpan.FromSeconds(TimeSpan.MaxValue.TotalSeconds);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With