Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a bug in TimeSpan?

Tags:

c#

timespan

This will output "0":

TimeSpan span = TimeSpan.Zero;
span.Add(TimeSpan.FromMinutes(5));
Console.WriteLine(span.TotalSeconds);   -----> 

However, this will output "300":

TimeSpan span = TimeSpan.Zero.Add(TimeSpan.FromMinutes(5));
Console.WriteLine(span.TotalSeconds);   -----> 

Is this a known bug?

like image 407
jojo Avatar asked Dec 10 '22 13:12

jojo


1 Answers

TimeSpan.Add does not modify the input - it returns a new TimeSpan which is the input plus the addend:

Remarks

The return value must be between TimeSpan.MinValue and TimeSpan.MaxValue; otherwise, an exception is thrown.

The return value is a new TimeSpan; the original TimeSpan is not modified.

like image 57
John Saunders Avatar answered Dec 24 '22 00:12

John Saunders