5.8. Understanding Automatic Properties

When .NET 3.5 was released, the C# language offered yet another way to define simple encapsulation services with minimal code, using automatic property syntax. To illustrate, create a new C# Console Application project named AutoProps. Now, insert a new C# class file (Car.cs) that defines the following class, which encapsulates a single point of data using classic property syntax.

// A Car type using standard property
// syntax.
class Car
{
  private string carName = string.Empty;
  public string PetName
{
    get { return carName; }
    set { carName = value; }
  }
}

While it is very true that most C# properties contain business rules within their set scope, it is not too uncommon that some properties literally have ...

Get Pro C# 2010 and the .NET 4 Platform, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.