CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jean-Paul S. Boodhoo

Develop With Passion

Automatic Properties - In case you didn't know

The following snippet of code (C# 3.5) is allowable, and gives you the benefit of unnecessary extra verbosity with the benefits of encapsulation. Of course, if you need to implement logic in your property, then you don't want to think about automatic properties:

public T Item { get; private set; }


Published Feb 20 2008, 12:25 AM by bitwisejp
Filed under:

Comments

Sean Feldman said:

I like this feature, it allows to keep something like DTOs very simple and short, also quickly refactorable with no multiple keystrokes. What about reflection, how that one digests automatic property backing field? normallly you shouldn't access backing field (as any other member field) directly, but what if you have to through reflection? 10x

# February 21, 2008 10:11 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 25, 2008 11:19 PM

Alexander Kojevnikov said:

JP, it's actually in C# 3.0 (and requires only .NET 2.0). Sorry for being a bore :)

Sean, the backing field's name is (pseudo)randomly generated by the compiler, you shouldn't access it using reflection.

# March 4, 2008 4:18 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!