BlackWaspTM
C# Programming
.NET 1.1+

C# Static Behaviour (2)

The fifth article in the C# Object-Oriented Programming tutorial describes the use of static methods, properties and constructors. These behaviours are accessible from classes without the requirement to instantiate new objects.

Static Properties

Static properties provide the functionality of standard properties, except that the property is not linked to any instance of the class. Properties can be read/write, read-only or write-only as with standard properties. As such, they can essentially be thought of as global variables.

Creating a Static Private Variable

In the earlier article describing class properties we created a class-level private variable to hold the data behind a property. As mentioned above, private variables may not be accessed by static methods, and this also applies in the case of static properties. Instead, we must create a static private variable if the property value is to be held rather than calculated.

In the MassCalculator class we will implement a call counter that is incremented every time the mass calculation method is used. The current value for the call counter is stored in a static private variable that may now be added within the class' code block:

private static int _callCount;

To maintain the counter, adjust the CalculateMass method so that it increments the variable on every call:

public static int CalculateMass(int density, int volume)
{
    _callCount++;
    return density * volume;
}

Exposing a Static Property

As you may expect, adding a static property to a class requires only that the declaration includes the 'static' keyword to modify the property's behaviour. We can now add the read-only call count property to the class.

public static int CallCount
{
    get
    {
        return _callCount;
    }
}

Using a Static Property

To use the static property, the property name is preceded by the class name and the member access operator (.). To demonstrate, adjust the Main method to perform two mass calculations and output the call count property as follows:

static void Main(string[] args)
{
    int density = 50;
    int volume = 100;
    int volume2 = 180;

    int mass1 = MassCalculator.CalculateMass(density, volume);
    int mass2 = MassCalculator.CalculateMass(density, volume2);
    int calls = MassCalculator.CallCount;

    Console.WriteLine("Mass1: {0}", mass1);         // Outputs "Mass1: 5000"
    Console.WriteLine("Mass2: {0}", mass2);         // Outputs "Mass2: 9000"
    Console.WriteLine("Calls: {0}", calls);         // Outputs "Calls: 2"
}

Static Constructors

A static constructor is used to initialise the static state of a class when it is first used. This is similar to a standard constructor with some exceptions. A static constructor is always declared as private and as such may not be directly called by a program. A static constructor therefore has no facility to add parameters. It is also not possible to include a static destructor.

To add a static constructor, create a private constructor with the static keyword as a prefix. the following code could be added to the MassCalculator class if the appropriate static methods were available to retrieve the previously saved call count from a file or other storage.

static MassCalculator()
{
    _callCount = InitialiseCallCount();
}
14 October 2007