Auto-implemented properties C# with Example



Auto-implemented properties C# with Example

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. 

0 Comment's

Comment Form

Submit Comment