Auto-Implemented Properties

C# 3.0 introduced a feature called auto-implemented properties to reduce the “syntactic ceremony” involved in creating “simple properties” that are nothing but smart fields. Instead of having to declare the backing field and a property accessing that field manually, the following syntax takes care of it all:

class Person{    public string Name { get; set; }    public int Age { get; set; }}

The preceding code is turned into this equivalent:

class Person{    private string <Name>k__BackingField;    private int <Age>k__BackingField;    public string Name    {        get { return <Name>k__BackingField; }        set { <Name>k__BackingField = value; }    }    public ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.