I see this quiet often in C# documentation. But what does it do?
public class Car { public Name { get; set; } }
Example explained The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property.
A get property accessor is used to return the property value, and a set property accessor is used to assign a new value.
The get keyword defines an accessor method in a property or indexer that returns the property value or the indexer element. For more information, see Properties, Auto-Implemented Properties and Indexers.
It is shorthand for:
private string _name; public string Name { get { return _name; } set { _name = value; } }
The compiler generates the member variable. This is called an automatic property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With