What are static members of a C# Class?

Static members in C# belong to the class itself rather than to any specific instance of the class. When you declare a member as static, only one copy exists regardless of how many objects are created from the class.

Static members are accessed using the class name directly, without creating an instance. They are commonly used for utility functions, constants, and shared data that should be consistent across all instances of a class.

Syntax

Following is the syntax for declaring static members −

public static dataType memberName;
public static returnType MethodName() {
   // method body
}

Static members are accessed using the class name −

ClassName.staticMember = value;
ClassName.StaticMethod();

Key Characteristics of Static Members

  • Shared across all instances − Only one copy exists for the entire class.

  • Accessed via class name − No object instantiation required.

  • Initialized once − Static variables are initialized when the class is first loaded.

  • Cannot access instance members − Static methods can only access other static members directly.

Static vs Instance Members Static Members ? One copy per class ? Shared by all objects ? Access: ClassName.Member static int count; Instance Members ? Copy per object ? Unique to each instance ? Access: objectName.Member int instanceVar;

Using Static Variables

Static variables maintain their value across all instances and method calls −

using System;

class StaticVar {
   public static int num;

   public void count() {
      num++;
   }

   public int getNum() {
      return num;
   }
}

class StaticTester {
   static void Main(string[] args) {
      StaticVar s1 = new StaticVar();
      StaticVar s2 = new StaticVar();

      s1.count();
      s1.count();
      s1.count();

      s2.count();
      s2.count();
      s2.count();

      Console.WriteLine("Variable num for s1: {0}", s1.getNum());
      Console.WriteLine("Variable num for s2: {0}", s2.getNum());
      Console.WriteLine("Direct access: {0}", StaticVar.num);
   }
}

The output of the above code is −

Variable num for s1: 6
Variable num for s2: 6
Direct access: 6

Using Static Methods

Static methods can be called without creating an instance of the class −

using System;

class Calculator {
   public static int Add(int a, int b) {
      return a + b;
   }

   public static int Multiply(int a, int b) {
      return a * b;
   }

   public static void ShowInfo() {
      Console.WriteLine("This is a static calculator class");
   }
}

class Program {
   static void Main(string[] args) {
      // No object creation needed
      int sum = Calculator.Add(10, 20);
      int product = Calculator.Multiply(5, 6);
      
      Console.WriteLine("Sum: " + sum);
      Console.WriteLine("Product: " + product);
      Calculator.ShowInfo();
   }
}

The output of the above code is −

Sum: 30
Product: 30
This is a static calculator class

Static Constructor

A static constructor initializes static data and is called automatically before any static member is accessed −

using System;

class Employee {
   public static int totalEmployees;
   public string name;

   static Employee() {
      totalEmployees = 0;
      Console.WriteLine("Static constructor called");
   }

   public Employee(string empName) {
      name = empName;
      totalEmployees++;
   }

   public static void ShowTotal() {
      Console.WriteLine("Total employees: " + totalEmployees);
   }
}

class Program {
   static void Main(string[] args) {
      Employee.ShowTotal(); // Static constructor runs here
      Employee emp1 = new Employee("Alice");
      Employee emp2 = new Employee("Bob");
      Employee.ShowTotal();
   }
}

The output of the above code is −

Static constructor called
Total employees: 0
Total employees: 2

Comparison: Static vs Instance Members

Static Members Instance Members
Belong to the class itself Belong to individual objects
Shared across all instances Unique to each instance
Accessed using class name Accessed using object reference
Cannot access instance members directly Can access both static and instance members
Memory allocated once when class loads Memory allocated for each object creation

Conclusion

Static members in C# are class-level entities that exist independently of any object instance. They provide a way to share data and functionality across all instances of a class, making them ideal for utility methods, constants, and shared counters that need to maintain state across the entire application.

Updated on: 2026-03-17T07:04:35+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements