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; }

This entry was posted in C#. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

2 Responses to Automatic Properties – In case you didn’t know

  1. 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.

  2. Sean Feldman says:

    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

Leave a Reply