DCSIMG
C# 3.0 New Feature for the Week #1: Automatic Properties - Doron's .NET Space

C# 3.0 New Feature for the Week #1: Automatic Properties

I've decided that I really need to get in the program and start learning C# 3.0 and Linq seriously. I've been planning to this for a long time, and even started playing with it a little, but never dug in really deep. So my new "C# 3.0 New Feature for the Week" series of posts is about getting myself to commit to studying and blogging about it. Every week I will present one new feature and talk about what is it good for and how it is implemented.

So here we go. The first feature we'll talk about is automatic properties. This is a cute little feature, meant to save us having to write this:

public class Person { private string _firstName; public string FirstName { get { return _firstName; } set { _firstName = value; } } private string _lastName; public string LastName { get { return _lastName; } set { _lastName = value; } } }

I mean, usually there's no logic inside the properties getters and setters, but since you like to write good and maintainable code, you use properties in case you'd like to add something in the future. But still, having to write a million of them can be quite a *** (well, at least if you don't have ReSharper. If you do, Alt+Insert is all you need).

So now what we can write is:

public class Person { public string FirstName {get; set;} public string LastName {get; set; } }

If we want, we can add access modifiers, like this:

public string FirstName {get; internal set;}

Isn't this so very nice and clean? If we now want to add something inside the getter or setter, we can convert it to a standard property (hopefully ReSharper 4 will have this much needed refactoring option) and do what we want.

How is it implemented?

As all new C# 3.0 features, automatic properties is a compiler feature, or syntactic sugar if you will. As the CLR is still at version 2.0, the conversion between automatic and standard properties is done for us by the compiler. Let's have a look at our class in the Reflector.

public class Person { // Fields [CompilerGenerated] private string <FirstName>k__BackingField; [CompilerGenerated] private string <LastName>k__BackingField; // Properties public string FirstName { [CompilerGenerated] get { return this.<FirstName>k__BackingField; } [CompilerGenerated] set { this.<FirstName>k__BackingField = value; } } public string LastName { [CompilerGenerated] get { return this.<LastName>k__BackingField; } [CompilerGenerated] set { this.<LastName>k__BackingField = value; } } }

As you might have guessed, the compiler adds two private properties for us and implements getters and setters.

Now, I know what you're thinking. What if I add a field called <FirstName>k__BackingField to my class? Will the compiler go berserk? Well, sadly, you can't. Field names (or any other name for that matter) cannot start with a "<", so this won't compile to begin with. Oh well.

This is it. Next week we'll talk about Object Initializers. Until then, happy coding.

Published Saturday, November 10, 2007 4:09 PM by dorony
תגים:,

Comments

# C# 3.0 New Feature for the Week #2: Object and Collection Initializers

Last week I started my series of posts about new C# 3.0 features, and my second post will be about object

Friday, November 16, 2007 8:16 PM by Doron's .NET Space

# re: C# 3.0 New Feature for the Week #1: Automatic Properties

That's pretty much the same trick used to implement anonymous methods in C# 2.0. Cute, but as you said - completely unnecessary if you have Resharper up your sleeve.

Saturday, November 24, 2007 6:00 PM by Idan Zeierman

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: