Here's a particularly nasty gotcha that new .NET developers should be aware of. It should *go without saying, but nonetheless it appears that it *does need to be said. Hmph.

The sordid details are not included here to preserve the dignity of the guilty parties, but basically it boils down to: a property getter/setter is NOT the same as a public instance variable.

In other words, you are never guaranteed that the value you put in will be the value you get back. Consider the following:


this.Foo = "Hello, world!";
Debug.Assert(this.Foo.Equals("Hello, world!");

If I were evil, my definition of Foo could be as below:


private string _foo;
public string Foo
{
get
{
return _foo;
}
set
{
Trace.WriteLine("The caller asked me to store '{0}', but I'm going to drop it on the floor instead. <snigger />".FormatWith(value));
}
}

As I said, this is an elementary distinction (and a trivial example) but do not not not assume that just because you've asked a setter to store a value that you'll get the same value back from the getter. You should, but "should" is the most over-used word in this industry.