TheDeveloperBlog.com


C# Static Property

Static property. A static property is similar to a static method. It uses the composite name to be accessed. Static properties use the same get and set tokens as instance properties. They are useful for abstracting global data in programs.


Example. First, this program uses static properties. It shows how to get and set static properties in a static class in a C# program. To use a static property with a backing store field, you must also specify that the backing store be static.

However: The program does not demonstrate this use. It shows the automatically implemented property syntax in the static bool property.

C# program that uses static properties

using System;

static class Settings
{
    public static int DayNumber
    {
	get
	{
	    return DateTime.Today.Day;
	}
    }

    public static string DayName
    {
	get
	{
	    return DateTime.Today.DayOfWeek.ToString();
	}
    }

    public static bool Finished
    {
	get;
	set;
    }
}

class Program
{
    static void Main()
    {
	//
	// Read the static properties.
	//
	Console.WriteLine(Settings.DayNumber);
	Console.WriteLine(Settings.DayName);
	//
	// Change the value of the static bool property.
	//
	Settings.Finished = true;
	Console.WriteLine(Settings.Finished);
    }
}

Output

13
Sunday
True

The Settings class is decorated with the static modifier and it cannot be instantiated. It has three static properties. Two of the static properties are read-only and only have the get accessor, while the third is writable as well.

Static classes have a restriction that they cannot be instantiated. This means you are less likely to needlessly create an instance of class that has no state. Static properties are a good function type to put inside a static class.

Static Class

Note: Many projects use a global variable class that often can be expressed as a static class with static properties.

Tip: Static properties can be accessed with the composite name, using the dot notation.


Discussion. Let's discuss the concept of global variables in programming languages generally and the C# language in specific. Global variables are useful and even functional languages use global environments for binding variables and function names.

However: Global variables can increase complexity and cognitive footprint. This can be dealt with by using accessor routines.

Global Variable

Thread-safety issues. Usually, threading problems will only occur if the values are being written to by at least one thread. Using static properties that are readonly is safe in practice.

And: Because the memory is only being read, there is no chance for it to be written at the wrong time.

Tip: One strategy for reducing threading problems is to initialize the values at startup, and then treat them as read-only.

Readonly Fields

Summary. We looked at static properties in the C# language, seeing how they can be declared in a static class, and then how you can get and set their values. We noted thread-safety issues and one way you can avoid threading problems.