Possible Duplicate:
Why use getters and setters?
I see this a fair bit in code examples…
private List<Car> cars;
public List<Car> Cars
{
get { return this.cars; }
set { this.cars = value; }
}
What’s the benefit of that over just:
public List<Car> cars;
?
Thank you.
this is a property, a property allows you to control the set/get operations and perform other tasks when a value is set or get.
For example you could check if the cars list is null in the get and in case is null you can create and assign a new List to cars then return cars.