BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Class Properties

The third article in the C# Object-Oriented Programming tutorial expands upon the previous creation of simple classes by introducing properties. Properties of a class allow instantiated objects to have state with each object controlling its own data.

Adding State

In the previous article of this series, I explained the basic syntax and process for creating a class that has methods as a part of its public interface. This allows the generation of simple classes with behaviour but without state. The state of an object describes its individual data or unique configuration. In the example of a car object, methods allow us to accelerate or decelerate but the state, described in properties, allows us to determine the actual speed and to describe one car object as red and another as blue. This information creates a real differentiation between the two objects.

In this article, we will create a new class to represent a rectangle shape. This class will define four properties: the rectangle's height, width, area and perimeter. To begin, create a new console application and add a new class named "Rectangle".

Adding A Property

Creating a Class-Level Variable

In many cases, the information that is made public via a property is held directly within the object as a variable. This variable has a scope that makes it visible to the entire class. This is not always the case however, as the information may be held in a database or other external source or may be calculated rather than stored. In the example class in this article, two of the property values will be held in variables and two will be calculated.

To define a class-scoped variable, the declaration is made within the class's code block but outside of any methods or properties. Although not required, it is useful to precede the variable declaration with the private keyword to make the code clear and easy to read. Using this syntax, we can add two integer variables to the Rectangle class to hold the height and width.

class Rectangle
{
    private int _width;
    private int _height;
}

NB: The use of lower camel case and an underscore (_) prefix is one naming standard for class-level variables. It is a useful, though not essential, convention.

Exposing a Property

Adding a property to a class is similar to adding a variable. In fact, it is possible to create a rudimentary property by adding a variable at the class scope and simply declaring it as public instead of private. This actually creates a public field. However, this is inadvisable as it means that the class will have no control over the values that the properties are set to and some standard .NET functionality is lost. To avoid these problems the property is declared with a code block as follows:

public int Width
{
}

Once declared, functionality must be added to the property. This is achieved with the use of get and set accessors. These create two code blocks that control how the property's value is retrieved or calculated when requested (get accessor) and how the property's value is validated, processed and stored when assigned a value (set accessor). The two accessors are added to the property using the get and set keywords, each with its own code block:

public int Width
{
    get
    {
    }
    set
    {
    }
}

To complete the Width method, we now need to add the code that processes the getting and setting of the properties. This is relatively simple for the get accessor. When the property value is requested, we will simply return the value from the class-level variable. To do this, we can use the return keyword with the same syntax as for any functional method.

public int Width
{
    get
    {
        return _width;
    }
    set
    {
    }
}

When using the set accessor, the value that the external object is assigning to the property is passed as a variable named "value". This can be thought of as similar to a method parameter even though its name is hidden.

For the Width property, we will validate the provided value before storing it. If the value is negative or is greater than one hundred, an exception will be thrown and the property will remain unchanged. This is possible because of the correct usage of get and set accessors. If a simple public variable had been created as described earlier, this level of control would not be available.

public int Width
{
    get
    {
        return _width;
    }
    set
    {
        if (value < 0 || value > 100)
        {
            throw new OverflowException();
        }

        _width = value;
    }
}

Before we try out the new property, let's add a second property for the height. The same validation rules apply:

public int Height
{
    get
    {
        return _height;
    }
    set
    {
        if (value < 0 || value > 100)
        {
            throw new OverflowException();
        }

        _height = value;
    }
}
27 September 2007