O'Reilly logo

Pro C# 2010 and the .NET 4 Platform, Fifth Edition by Andrew Troelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required