C# 6.0 Suggestion: Backing Field Access

I just posted on one feature that I would like to see in C# 6.0, automatic property initializers. Another that I would like to see is some kind of reserved word access to the automatic backing properties that are created. This would actually allow for more complex property initializer scenarios without actually having to create the backing field.

public IEnumerable<Address> Addresses
return field.Where(x => x.IsActive);
} new Collection<Address>();

This example builds on the automatic property initializer that I have proposed previously but adds a parameter to the get accessor which can then be used to operate against the backing field. The backing field type is determined by the initializer.


7 thoughts on “C# 6.0 Suggestion: Backing Field Access

  1. Pingback: C# 6.0 Suggestion: Backing Field Type Declaration | Mitch Denny

  2. Pingback: C# 6.0 Suggestions: Quick Review | Mitch Denny

  3. Wahid Shalaly (@waheedsayed)

    Hi Mitch,

    It’s a nice idea to suggest what I’d like to see in next C# version. My lazy mind didn’t think about it 🙂

    This version of initializer is better than the first one, which was confusing, but I think there are other clearer formats we can compare to.
    public IEnumerable Addresses { get; set; } = new Collection();
    public IEnumerable Addresses { get; set; default { return new Collection(); } }

    I wonder about the reason for accessing the backing field while you have access to the property itself!
    Both should be equal at any moment since that backing field is internal implementation detail and what we care about is our property.

    Also, get(field) { return field.Where(x => x.IsActive); }, this retrieves active addresses only!
    What about Inactive ones?! By this there will be no means to access them.


  4. Mitch Denny Post author

    Hi Wahid,

    Thanks for responding to my post. You are right – in a lot of cases, access to the backing field isn’t going to be that useful. The scenario I gave what pretty contrived. How about another one, performing some kind of logging when the property is set. Access to the backing field helps here because a) you don’t have to create the backing field yourself, b) don’t have to deal with the namespace pollution that entails. What would be really cool is if the getter or setter method body could actually be interpreted in some cases as an expression which could then be directly consumed by ORMs such as EF as a filter on the underlying related set of objects.

  5. Thomas Freudenberg

    If the Addresses property returns only addresses with IsActive = true, and because this is an automatic property, there’s no way to access any items in Addresses which IsActive property is false.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s