Creating Properties

Properties are much like fields as far as code outside your object is concerned, but internally, they use accessor methods to get and set their data, giving you the chance to add code that restricts what data is written to and read from a property.

For example, we have a class named Customer in which we want to support a property called Name, which holds the customer's name. To store that name, we'll use a private field called name:

class Customer
{
  private string name;
    .
    .
    .
}

FOR C++ PROGRAMMERS

Properties do not formally exist in C++.

To implement a property, you set up get and set accessor methods; the get method returns the property's value, and the set method sets it. Here's how to implement the get accessor method ...

Get Microsoft® Visual C#® .NET 2003 Kick Start 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.