Jean-Paul S. Boodhoo

Sponsors

The Lounge

Wicked Cool Jobs

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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; }


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

[Advertisement]

Comments

Sean Feldman wrote re: Automatic Properties - In case you didn't know
on Thu, Feb 21 2008 10:11 PM

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

DotNetKicks.com wrote Automatic Properties
on Mon, Feb 25 2008 11:19 PM

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

Alexander Kojevnikov wrote re: Automatic Properties - In case you didn't know
on Tue, Mar 4 2008 4:18 AM

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.

Devlicio.us