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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.