CSharp - Property Automatic properties

Introduction

The most common implementation for a property is a getter and/or setter that reads and writes to a private field of the same type as the property.

An automatic property declaration instructs the compiler to provide this implementation.

The following code declares CurrentRadius as an automatic property:

class Circle
{
       public decimal CurrentRadius { get; set; }
}

The compiler automatically generates a private backing field for CurrentRadius.

You cannot access that generated field.

The set accessor can be marked private or protected to expose the property as read-only.

Related Topics