What is the worst C#/.NET gotcha?

Posted by MusiGenesis on Stack Overflow See other posts from Stack Overflow or by MusiGenesis
Published on 2008-10-27T19:30:08Z Indexed on 2010/03/16 18:01 UTC
Read the original article Hit count: 471

Filed under:
|
|
|

This question is similar to this one, but focused on C# and .NET.

I was recently working with a DateTime object, and wrote something like this:

DateTime dt = DateTime.Now;
dt.AddDays(1);
return dt; // still today's date! WTF?

The intellisense documentation for AddDays says it adds a day to the date, which it doesn't - it actually returns a date with a day added to it, so you have to write it like:

DateTime dt = DateTime.Now;
dt = dt.AddDays(1);
return dt; // tomorrow's date

This one has bitten me a number of times before, so I thought it would be useful to catalog the worst C# gotchas.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET