#1,218 – C# 6.0 – Using Lambdas for Getter-Only Auto-Properties

In addition to using lambda expressions for method bodies in C# 6.0, you can use a lambda expression 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 lambda expression tells the compiler that this is a property with a getter, rather than a field.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

4 Responses to #1,218 – C# 6.0 – Using Lambdas for Getter-Only Auto-Properties

  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?

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

%d bloggers like this: