One Thing I Don't Like About Automatic Properties

I glanced at this newsgroup post in my favorite RSS reader, and for some reason that one sentence prompted my brain to rant a bit on automatic properties.

Basically, automatic properties look like this:

public sealed class Properties
{
    public Guid Id
    {
        get;
        private set;
    }
}

What C# does behind the scenes for you is create a private field with a mangled name - it looks like this (copied straight from Reflector):

[CompilerGenerated]
private Guid <Id>k__BackingField;

public Guid Id
{
    [CompilerGenerated]
    get
    {
        return this.<Id>k__BackingField;
    }
    [CompilerGenerated]
    private set
    {
        this.<Id>k__BackingField = value;
    }
}

Basically, the field name is the name of the property surrounded with brackets and appended with the "k__BackingField" string.

For simple read/write properties, automatic properties do the job. But the issue I have is that it's inconsistent. Namely, if I need to change how the getter or setter works, I can't use automatic properties anymore; I have to resort to writing the logic myself:

public sealed class Properties
{
    private Guid settableId;
    
    public Guid Id
    {
        get;
        private set;
    }
    
    public Guid SettableId
    {
        get
        {
            return this.settableId;
        }
        set
        {
            if(value == Guid.Empty)
            {
                throw new ArgumentException("The ID cannot be empty.", "value");
            }
            
            this.settableId = value;
        }
    }
}

Wouldn't it be way cool if C# would have a keyword that would represent the backing field? Then you could do this:

public sealed class Properties
{
    public Guid Id
    {
        get;
        private set;
    }
    
    public Guid SettableId
    {
        get;
        set
        {
            if(value == Guid.Empty)
            {
                throw new ArgumentException("The ID cannot be empty.", "value");
            }
            
            this.autofield = value;
        }
    }
}

That feels like the best of both worlds. You can use the standard get/set implementations, but if you want to customize one or the other (or both), then you're free to do so. This pretty much eliminates the need for the developer to explicitly define fields that backs a property. Maybe "autofield" isn't the best syntax, but you get the point - have a keyword that represents the backing field in a simplistic way.

I kind of like this, but it's a Friday and I haven't had enough coffee to think this through, so there may be gaping holes in my design. But I do hope automatic properties are updated in a future version of C#.

* Posted at 10.03.2008 08:37:02 AM (Last Update: 10.05.2008 02:59:05 PM) | 8 comments | Link | RSS *

Comments

# ran into that, from Jake Good at 10.03.2008 09:19:21 AM

I ran into that yesterday while playing around with some code... creepy!

# True, from The Felkeroni at 10.03.2008 10:24:06 AM

I had this same feeling the other day while working on some code. I had to add some logic in the setter. Kind of irritated me that I had to write the getter logic at that point. Oh well.. its on the right path. Maybe in VS2010...

# make a DSL, from Justin Chase at 10.03.2008 12:53:07 PM

You could do that in boo pretty easily.

# And..., from Jason Bock at 10.03.2008 12:54:35 PM

Justin, Since we're not talking about Boo, how is that relevant to the conversation at hand? I'm not trying to sound oreny, but each language can do something that another cannot.

# He..., from Gim at 10.03.2008 07:16:59 PM

"how is that relevant to the conversation at hand"

The poster is proving that he knows stuff.

# There is a refactoring to help..., from Chris Brandsma at 10.03.2008 07:20:37 PM

There is a refactoring to help with that in ReSharper called "To property with backing field", you get it with an alt-enter.

But I still agree with you, I shouldn't have to refactor that much to get there.

# Software Engineer, from Jeff LeBert at 10.04.2008 01:04:01 PM

I found automatic properties fairly useless for the same reason you pointed out and also that you can't do readonly. I love the readonly keyword and hate that I can't use it with automatic properties.

I think a good syntax instead of this.autofield is to just use the property name. You code would look like inside the setter:
this.SettableId = value;
The only problem with this is that it looks like a recursive call. If we ignore that part and just modify the C# compiler to translate the code into setting the compiler generated backing field then life would be great. Of course, the compiler would only do this inside the setter.

I was thinking we could have a similar solution for the readonly issue. Mark the property as readonly and don't specify a setter. In the constuctor, and only in the constructor, you could use this.SettableId to set the value. This is basically what readonly does to a field anyway.

And now, to make this comment non-relevant, I like boo. ;-)

# Responses, from Jason Bock at 10.05.2008 02:59:01 PM

Gim,

I can't tell if you're serious or kidding.

Chris,

Good point, but that's using a 3rd-party tool. Hopefully in a future version they refactor this feature a bit.

Jeff,

You had me until you said, "I like boo" :P

Add a Comment

(*) = Required field
Name (*):

E-Mail (*):

Web Site:

Title (*):

Comments (*):

Enter the code you see (*)



Quote
"Documentation is like sex; when it's good, it's very, very good, and when it's bad, it's better than nothing." Dick Brandon
Twitter History
follow me on Twitter
Blog History