TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Static Property

This C# example program uses static property members. It shows get and set.

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf