Working With Static In C#

C# Static

This article is all about static keyword in C# and how to use static in C# applications. This article covers what a static class is in C#, what is the purpose of static members and how static is used in C#. You'll also learn about static constructors, static methods and properties with examples of using static.

What is static in C#

The static keyword can be applied to class members including constructors, methods, properties, and events. Static can also be applied to classes. The static modifier makes a member non-instantiable. The members of the class can be accessed via the class name (type) only. 

Static members in C#

Static members in a C# class is declared using the static keyword before the member's name with other modifiers. The purpose of using static types to have only one copy of a variable in memory. The last value assigned to the member is reflected everywhere a static member is used.
A class is loaded when either
  • The static members of the class is accessed for the first time, or
  • When the first instance of the class is created.
A class once loaded will remain in memory permanently and also are all static members of that class.
For every new instance of a class, all the instance members are allocated memory as one unit, but static field members of a class are allocated memory only once, irrespective of the number of objects created, and they are allocated memory when the class is loaded. These members are also called class members and are accessed outside the class using the class name.
A public static member of a class can be used as a Global member of the application because it can be assessed using the class names from any part of the application.

Static Constructor in C#

  • It is a constructor with a static keyword.
  • It is used to initialize static members dynamically and is executed when the class is loaded.
  • It is invoked by the CLR when the class is loaded by it and hence cannot be overloaded nor can be declared with any accesses specifier like a public or private.
Series of events that occur when the first object is created,
  1. The class is loaded.
  2. Static members are loaded and allocated memory.
  3. A static constructor is executed.
  4. The object is created and Instance members are loaded and allocated memory.
  5. Instance constructor is executed.
  6. 4 and 5 repeats for the subsequent objects.
Static Constructor cannot access Instance members of the class. This is because when the static constructor is executed the instance members are not allocated any memory. A static constructor can't be parameterized.

Static Methods

A method that does not have anything to do with the state of the object can be marked as a static method. Static methods and properties can only access static fields and static events.
Here is an example
  1. class File {  
  2.     public string srcPath;  
  3.     public void Copy(string destPath) {...  
  4.     }  
  5.     public static void Copy(string srcPath, string destPath) {...  
  6.     }  
  7. }   
All methods and properties (procedures) of the class irrespective of whether they are static or not are allocated memory for instructions only once. 
  1. Outside the class, static members of a class can be accessed using class names whereas the instance member of a class must be accessed using a reference variable referring to an object of that class.
  2. Instance members of the class cannot be accessed in a static constructor or any other static method or property of the class (unless it is qualified by a reference variable referring to an object of that class).
    For Example,
    1. public static void Foo() {  
    2.     //this._Balance = 100; // Is Invalid  
    3.     Accout a = new Account();  
    4.     a._Balance = 100; // IsValid because _Balance is qualified by “a” which is reference to an object.  
  1. An instance member can access static members of the class. 
  2. “this” cannot be used in the static member of a class.

Static Class

A class in C# can be declared as static and such a class can have only static members and instance members are not allowed. Also, such a class cannot be instantiated.
It is ideally used for managing Global data.
  1. static class Demo {  
  2.     public static int P1;  
  3.     public static void Foo() {}  
  4. }  
If a static class contains non- static members, it gives an error.
Static In C# 

Example using static

Here is a complete C# program that uses a static class with static fields, static properties, a static constructor, and a static method.
  1. using System;  
  2. namespace Article__Static {  
  3.     static class Program {  
  4.         static string name;  
  5.         private static string _subject;  
  6.         public static string subject {  
  7.             get {  
  8.                 return _subject;  
  9.             }  
  10.             set {  
  11.                 _subject = value;  
  12.             }  
  13.         }  
  14.         static Program() {  
  15.             Console.WriteLine("static constructor.");  
  16.             name = "static member.”  
  17.         }  
  18.         public static void Display() {  
  19.             Console.WriteLine("static method.” + name);   
  20.             }  
  21.             static void Main(string[] args) {  
  22.                 Program.subject = "CSharp";  
  23.                 Console.WriteLine(Program.subject);  
  24.                 Program.Display();  
  25.             }  
  26.         }  
  27.     }  
Static In C#


In this article, we learned all about static in C#. The C# static keyword is used to declare static classes, static members, static methods, and static properties.