Reputation: 3051
i have question about why should we use return in get,if dont use what happend?.plz see the code below:
private int _NumberOfDoors= 4;
public int NumberOfDoors
{
get
{
return _NumberOfDoors;
}
Upvotes: 0
Views: 99
Reputation: 10015
Using properties is a best practice to access your local variables. More info on properties (and get/set) can be found on MSDN Properties article
A small extract from there:
"The get keyword defines an accessor method in a property or indexer that retrieves the value of the property or the indexer element"
It retrieves the value, so you need to use the return
keyword.
"The set keyword defines an accessor method in a property or indexer that assigns the value of the property or the indexer element."
Set assigns the value, so you'll use varX = value;
Upvotes: 1
Reputation: 29496
get
is just a method that returns a value, so you have to return a value. If you don't your code won't compile! You could of course have the get
automatically implemented for you:
public int NumberOfDoors
{
get;
}
This is a read-only property.
Upvotes: 5