Static Class and Static Members in C#

When a Class has Static Keyword then it act as a Static Class.
Static Class and its members do not need any Object to call them, you can use the class name directly to call any members.

The Static class and its members enable you to access the data and member functions of the class without creating an instance of the Class (Object of the Class).

When we create any member to Static that means it share the same memory location.

Static members do not need to be instantiated. you can use a Static Class if data or methods declared in the class do not depend on the behaviour of an Object.

Code Example of Static Class

When the Class has Static keyword then that class is act as a Static Class as shown in below code , I have created a Student class with Static Keyword and inside this Student Class StudentId and StudentName are two static properties and one Static parameterless constructor and also has one Static method with name PrintName().

Image is not available

public static class Student 
{
    public static int StudentId;
    public static string StudentName=string.Empty;
    
    static Student()
    {
      StudentId = 1;
      StudentName = "James Berry";
    }

    public static void PrintName()
    {
      Console.WriteLine("Your Name is :-", 
      StudentName);
    }
} 

Note :- We can not declare instance members in static class , it only have static members as shown in below image , I have declared StudentId as a instance member and it is throwing compile time error.

Image is not available

Note :- We can not declare instance Constructor in static class , it only have static Constructors as shown in below image , I have declared one instance constructor and it is throwing compile time error.

Image is not available

Note :- We can not declare parameterized Static Constructor in static class , it only have parameterless static Constructors as shown in below image , I have declared one parameterized Static constructor and it is throwing compile time error.

Image is not available

Note :- We Can not create Object/Instance of Static Class with New Keywords. as shown in below image , I have created one instance of Static Class Student and it is throwing compile time error.

Image is not available

Note :- We can access members of Static Class using the ClassName as shown in below image, Employee is the Static Class and EmployeeId is the member of Static Class Employee.

Image is not available

Points to Remember :-

  • When a class member includes a static modifier then the member is called a Static Member.When no static modifier is present the member is called as Non Static member or instance Member.
  • Static Members are invoked Using Class Name whereas instance members are invoked using Objects of the Class.
  • An instance member is belongs to a specific Objects of a Class.If I create 3 objects of a class , i will have 3 sets of instance members in the memory whereas there will ever be only one copy of static member, no matter how many intances of a class are created.
  • A Class that is static indicates that its members are also static.
  • We can not instantiate a static class using New Keyword.
  • .NET Framework CLR Loads the static classes automatically when the program or Namespace which contains the static class is loaded.
  • Static constructors are used to initializes static fields in a Class.
  • You can declare a static constructor by using the keyowrd Static infront of the constructor name.
  • Static constructor is called only once ,no matter how many instances you create.
  • Static Constructors are called before instance constructors.
  • Static Classes are Sealed which means that you can not inherit this classes.
  • Static class members are initialized before they are accessed for the first time or before the static constructor is called.
  • Static members belongs to a class this means that a static member can be called by a class and by the Object of the class.
  • Non Static members belongs to an Object of a class.This means that a non-static member can be called only by the object of that Class.
  • Static Mambers accesses only other static members of a class.
  • Non Static members accesses both static and non static members of a Class.
  • We can not use this keyword with static members.

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.