MusiGenesis MusiGenesis - 1 month ago 9x
C# Question

What is the worst gotcha in C# or .NET?

I was recently working with a

object, and wrote something like this:

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

The intellisense documentation for
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.

private int myVar;
public int MyVar
    get { return MyVar; }

Blammo. Your app crashes with no stack trace. Happens all the time.

(Notice capital MyVar instead of lowercase myVar in the getter.)