Recently, I wrote code for adding seconds to a DateTime instance something like this:

DateTime time = DateTime.Now;
time.AddSeconds(100);

In the first round of unit test, I realize that it is not working correctly. A quick reference to MSDN revealed the fact:

“This method does not change the value of this DateTime. Instead, it returns a new DateTime whose value is the result of this operation.

The fractional part of value is the fractional part of a second. For example, 4.5 is equivalent to 4 seconds, 500 milliseconds, and 0 ticks.

The value parameter is rounded to the nearest millisecond.”

This means AddSeconds was creating a new DateTime instance rather than adding seconds to current instance. The correct code is something like this:

DateTime time = DateTime.Now;
time = time.AddSeconds(100);

This is very similar to addition operator.

DateTime time = DateTime.Now;
time = time + new TimeSpan(0, 0, 100);
DateTime time = DateTime.Now;
time += new TimeSpan(0, 0, 100);

This is actually true for all Add methods (Add, AddSeconds, AdddMinutes, AddHours, AddMilliseconds, …) in the DateTime class.

I think my initial confusion was caused by my frequent usage of Add methods in collection classes.

Hope this will save some of your debugging effort.