#1,218 – C# 6.0 – Using Expression-Bodied Property Getters

In addition to using expression-body definitions for method bodies in C# 6.0, you can use an expression-body definition to implement the getter of a read-only auto-property.

For example:

        public string Name { get; protected set; }
        public int Age { get; set; }

        public string BackwardsName => new string(Name.Reverse().ToArray());

The presence of the expression-body definition tells the compiler that this is a property with a getter, rather than a field.


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

5 Responses to #1,218 – C# 6.0 – Using Expression-Bodied Property Getters

  1. Pingback: Dew Drop – November 4, 2014 (#1891) | Morning Dew

  2. Asad says:

    This is absolutely beautiful.

  3. Jan says:

    This seems to be inconsistent! getter-only properties should have a readonly backing field (https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#getter-only-auto-properties). So how does that work with those lambdas?

  4. Anshika says:

    not working for me.. getting error

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: