// Error! Setting the PetName is only possible
// from within the Car type or by a child type!
c.PetName = "Frank";
// Getting the value is still OK.
Console.WriteLine("Your car is named {0}? That's odd...",
c.PetName);
Console.ReadLine();
}
Regarding Automatic Properties and Default Values
When you use automatic properties to encapsulate numerical or Boolean data, you are able to use
the autogenerated type properties straightaway within your code base, as the hidden backing fields
will be assigned a safe default value that can be used directly. However, be very aware that if you use
automatic property syntax to wrap a reference type, the hidden private reference type will also be
set to a default value of null:
class Garage
{
Do'stlaringiz bilan baham: |