Post

Automatic properties with custom logic

In General, Tech/Hacks on October 24, 2008 by Sid Tagged: , , ,

There should be a compiler supported way in C# to declare a property with custom getter/setter logic without having to explicitly declare a backing field. In the absence of such a feature, if you want to have custom getter/setter logic you have to ditch automatic properties, declare a backing field explicitly and then there’s nothing stopping a future programmer from assigning directly to the backing field and the custom getter/setter logic not getting executed probably leading to bugs.

Update:Check out part 2 where I take an example to bring the problem into perspective and propose a solution…

2 Responses to “Automatic properties with custom logic”

  1. If you make your backing field private then future programmer can not miss it. Isn’t it?

  2. I was talking about a future programmer modifying the same class where the private backing field is declared.

Leave a Reply