Some Creativity

Weblog of Siddharth Uppal

Automatic properties with custom logic

with 2 comments

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…

Written by Sid

October 24, 2008 at 12:08 pm

Posted in General, Tech/Hacks

Tagged with , , ,

2 Responses to 'Automatic properties with custom logic'

Subscribe to comments with RSS or TrackBack to 'Automatic properties with custom logic'.

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

    phpkid

    24 Oct 08 at 12:47 pm

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

    Sid

    24 Oct 08 at 3:07 pm

Leave a Reply