2008-04-14

"Why Properties Matter"

A colleague recently presented me with code that used a public field. I basically told him not to do that, but when he asked why not, I was unable to quickly formulate a convincing answer, and mumbled something along the lines of "changing a field to a property if you need to later is a breaking change". This was one of those trivial properties in a class that wasn't a component — pretty harmless using a field, right?

Jon Skeet has formulated the case against in a quite definitive way, and these were the objections I was thinking about that I couldn't immediately present my colleague with. Read C# in Depth: Why Properties Matter and you'll be able to convincingly tell people why properties are a better idea than fields.

2 comments:

Robbie said...

Yeah! Tell him! ;)

The link to the C# in Depth page isn't working for me by the way.

Jeroen Mostert said...

The site seems to be down at the moment. I'm sure it's only temporary.