Auto-implemented properties

suggest change

Auto-implemented properties were introduced in C# 3.

An auto-implemented property is declared with an empty getter and setter (accessors):

public bool IsValid { get; set; }

When an auto-implemented property is written in your code, the compiler creates a private anonymous field that can only be accessed through the property’s accessors.

The above auto-implemented property statement is equivalent to writing this lengthy code:

private bool _isValid;
public bool IsValid
{
    get { return _isValid; }
    set { _isValid = value; }
}

Auto-implemented properties cannot have any logic in their accessors, for example:

public bool IsValid { get; set { PropertyChanged("IsValid"); } } // Invalid code

An auto-implemented property can however have different access modifiers for its accessors:

public bool IsValid { get; private set; }

C# 6 allows auto-implemented properties to have no setter at all (making it immutable, since its value can be set only inside the constructor or hard coded):

public bool IsValid { get; }    
public bool IsValid { get; } = true;

For more information on initializing auto-implemented properties, read the Auto-property initializers documentation.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Properties:
* Auto-implemented properties

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
85 Properties
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script