C# 6.0 Suggestion: Automatic Property Initializers

When automatic properties were introduced into C# as a short-cut way of declaring properties I am sure lots of developers cheered for joy. Automatic properties are a great enhancement but every-time I use them I keep thinking that they are somewhat incomplete.

What I would like to do is have a neat way initializing automatic properties without having to declare them in the constructor. The syntax would be something like as follows:

public class Customer
{
public IEnumerable<Address> Addresses { get; protected set; } new Collection<Address>();
}

There are a few subtle things going on here. First, I might want to expose a list of addresses to calling code, however I may not necessarily want to expose a collection that can be updated. The syntax above exposes a public property as IEnumerable<Address>, however the backing field would be created as a Collection<Address>.

This would remove probably the #1 reason that I have to resort to implementing a property manually.

6 thoughts on “C# 6.0 Suggestion: Automatic Property Initializers

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

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

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

  4. Mitch Denny Post author

    Hi zproxy,

    I agree about the use of the “as” keyword to specify backing field type, however I would probably present the syntax slightly differently:

    public IEnumerable

    Addresses { get; set; } new Collection
    as IEnumerable;

    Kind of dovetails into some of the other posts I put up tonight. I’m generally not in favour of introducing a “property” keyword simply because:

    a) Adding keywords can be dangerous.
    b) it really isn’t necessary since { get; set; } is pretty concise already.

    But thanks for contributing, its nice to now folks get what I am talking about. I think we only differ on the syntax, not the intent here.

  5. Bob

    I know it’s an old thread, but hey..🙂

    I have also longed for this in my dto’s, having to initialise collections in the constructor is such a pain.

    Honestly tho, I’d like a different syntax again🙂

    public IList Addresses {get; protected set;} = new List();

    ie, with the = operator, as that keeps the familiarity of assignment.

    Alternatively, if that breaks thelanguage parser badly, coudl do this:

    public IList Addresses {get; protected set;} with default new List();

    I guess code is generally moving to a more readable and verbose form, maybe this would be more in vogue😛

    But I’d like the first one🙂

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s