Class and Static Variables in C#

In C#, static variables belong to the class itself rather than to any specific instance of the class. They are shared among all instances of the class and can be accessed using the class name without creating an object. Class variables (instance variables) belong to individual objects and each instance has its own copy of these variables.

Static Variables

Static variables are declared using the static keyword and are useful for defining constants or shared data across all instances of a class.

Syntax

public static dataType variableName;

Static vs Instance Variables Static Variable Shared by all instances Accessed via class name All objects share same memory location Instance Variable Unique per instance Accessed via object Each object has separate copies

Example

using System;

namespace StaticVarApplication {
   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());
      }
   }
}

The output of the above code is −

Variable num for s1: 6
Variable num for s2: 6

Notice how both objects show the same value (6) because they share the same static variable num.

Instance Variables (Class Variables)

Instance variables are the attributes of an object that are kept private to implement encapsulation. These variables can only be accessed using public member functions and each instance has its own copy.

Example

using System;

namespace BoxApplication {
   class Box {
      private double length;   // Length of a box
      private double breadth;  // Breadth of a box
      private double height;   // Height of a box

      public void setLength( double len ) {
         length = len;
      }
      public void setBreadth( double bre ) {
         breadth = bre;
      }
      public void setHeight( double hei ) {
         height = hei;
      }
      public double getVolume() {
         return length * breadth * height;
      }
   }
   class Boxtester {
      static void Main(string[] args) {
         Box Box1 = new Box(); // Declare Box1 of type Box
         Box Box2 = new Box();
         double volume;

         // Declare Box2 of type Box
         // box 1 specification
         Box1.setLength(6.0);
         Box1.setBreadth(7.0);
         Box1.setHeight(5.0);

         // box 2 specification
         Box2.setLength(12.0);
         Box2.setBreadth(13.0);
         Box2.setHeight(10.0);

         // volume of box 1
         volume = Box1.getVolume();
         Console.WriteLine("Volume of Box1 : {0}" ,volume);

         // volume of box 2
         volume = Box2.getVolume();
         Console.WriteLine("Volume of Box2 : {0}", volume);
      }
   }
}

The output of the above code is −

Volume of Box1 : 210
Volume of Box2 : 1560

Comparison

Static Variables Instance Variables
Belong to the class itself Belong to individual objects
Shared among all instances Each instance has its own copy
Accessed using class name Accessed using object reference
Initialized when class is first loaded Initialized when object is created
Memory allocated once Memory allocated per instance

Conclusion

Static variables in C# are shared across all instances of a class and are useful for constants or shared data, while instance variables belong to individual objects and maintain separate values for each instance. Understanding the difference helps in proper memory management and design patterns.

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

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements