C# Static Keyword
This article is about the static keyword in C# and how to use static in C# applications. This article covers what a static class is in C#, 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.
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 class members can only be accessed via the class name (type).
Static members in C#
Static members in a C# class are declared using the static keyword before the member's name with other modifiers. The purpose of using static types is to have only one copy of a variable in memory. Therefore, 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 are accessed for the first time, or
- When the first instance of the class is created.
- Once a class is loaded, it will remain in memory permanently, and 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 and hence cannot be overloaded nor declared with any accesses specifier like a public or private.
Series of events that occur when the first object is created,
- The class is loaded.
- Static members are loaded and allocated memory.
- A static constructor is executed.
- The object is created, and Instance members are loaded and allocated memory.
- Instance constructor is executed.
- 4 and 5 repeats for the subsequent objects.
Note
Static constructors cannot access Instance members of the class. This is because when the static constructor is executed, the instance members are not allocated any memory. Therefore, a static constructor can't be parameterized.



Raju PaladiyaPosted Feb 19, 2020, 1:28 PM
Awesome !! Thank you for sharing.
KiaraPosted Nov 12, 2018, 12:49 AM
Interesting..
VinayPosted Oct 30, 2018, 12:06 AM
Nice article
Sana DhananiPosted Oct 23, 2018, 11:30 PM
Awesome article! Your article covered EVERYTHING about static. It cleared many doubts for me in understanding the subject :)
Rushi MehtaPosted Oct 23, 2018, 11:20 PM
Nice explain Article..
Abhishek MishraPosted Oct 23, 2018, 10:41 AM
Great article as always sir. I am a big fan of your lectures. I loved your Azure sessions