Working With Static In C#

The static keyword in C# declares a data member static. A static member in C# cannot be instantiated and accessed via the class name. Static modifier can be applied on data members, constructors, classes, methods, properties, and events.

Static in CSharp 

Introduction
 
This article is all about static keyword in C# and how to use it. The article exaplains: 
  • Static members
  • Static constructor
  • Static methods and properties
  • Static class
  • Examples of using static

Description

The static keyword can be applied to a class members including constructors, methods, properties, and events. Static can also be applied to claases. The static modifier makes an a member non-instantiable. The members of the class can be accessed via the class name (type) only. 
 
Static members in C# - When are they loaded and how is the memory allocation

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 class name.

A public static member of a class can be used as Global member of the application because it can be assessed using class name from any part of the application.

Static Constructor in C#
  • It is a constructor with 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 public or private.

Series of events that occur when the first object is created,

  1. Class is loaded.
  2. Static members are loaded and allocated memory.
  3. Static constructor is executed.
  4. Object is created and Instance members are loaded and allocated memory.
  5. Instance constructor is executed.
  6. 4 and 5 repeat for subsequent object.

Note: 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. Static constructor can't be parameterized.

 
Static Methods

A method which does not have anything to do with the state of the object can be marked as 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. }   
Note: 

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 a static members of a class can be accessed using class name 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. }  

Example

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 propertiers, 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.     }  

Output

Static In C#