// Must use constructors to override default
// values assigned to hidden backing fields.
public Garage()
{
MyAuto = new Car();
NumberOfCars = 1;
}
public Garage(Car car, int number)
{
MyAuto = car;
NumberOfCars = number;
}
}
As you most likely agree, this is a very nice extension to the C# programming language, as you
can define a number of properties for a class using a streamlined syntax. Be aware of course that if
you are building a property that requires additional code beyond getting and setting the underlying
private field (such as data validation logic, writing to an event log, communicating with a database,
etc.), you will be required to define a “normal” .NET property type by hand. C# 2008 automatic
properties never do more than provide simple encapsulation for an underlying data type.
Do'stlaringiz bilan baham: |