(C# Language) static and const Members

Only one copy of a static data member exists in the entire program. A const member behaves like a static, but it's value is marked as fixed and constant. The compiler knows its value. Therefore it can make substitutions and avoid memory allocation for a const member. Let's see the finer details.
(Rev. 07-Nov-2022)

Categories | About |     |  


Table of Contents (top down ↓)

Quick Summary

Here's a quick summary of everything discussed in this tutorial. You can read the remaining article for finer detail.

  1. static Members: one copy of a static data member exists in the entire program - even if 100 objects of the class exist.
  2. instance Members: multiple copies exist of instance members - one per object.
  3. const Members: values known at compile time and remain fixed, so compiler makes substitutions and saves memory allocations for constant members.

Video Explanation (see it happen!)

Please watch the following youtube video:

static Data Members of a class

static data members are accessed by the name of the class. They are members of the class, not of an object or instance of that class.

Consider a class that contains a static member called Count. This member is a static member. So compiler will create it only once.

Next we have added an instance member called mName. This member will be created whenever an object of this class is created. It is meant to hold data for the object. You can think of it as a personal member.

So here we have a contrast here. A static member holds data for a class whereas an instance member holds data for its object. Secondly, there is only one static member even if you create 20 objects of this class. But there will be 20 instance members if 20 objects of this class are created. Each object has its own personal mName.

// example of an object counter class 

class MyClass
  public static int Count;

  public String mName;

  // not necessary here because 
  // Count will be set to zero 
  // even without this 
  static MyClass()
    Count = 0;

  public MyClass(string name)
    mName = name;


After that we have added a static constructor to initialize static members of this class. We have set Count = 0. It has been done just for the sake of it. Runtime was, in any case, going to set it to its default value, which is zero.

Next we add a parameterized constructor to set the value of mName, and to increment the static member Count.

There is only one static member called Count. This means that we can now keep a count of the number of objects created so far. The same Count is incremented whenever an object of this class is created.

// main function to test the counter 
static void Main()
  MyClass obj1 = new MyClass("name1");

  // prints 1 
  Console.WriteLine($"Created so far = {MyClass.Count}");

  MyClass obj2 = new MyClass("name2");

  // prints 2 
  Console.WriteLine($"Created so far = {MyClass.Count}");


I have attached the source code to the downloads of this tutorial.

Next we have the main function where we create an object of the class. We can now display the value of the static member by using the name of the class MyClass.Count. It should print 1 because we have created one object so far.

A second object is now created, and again the count printed. We expect the output to be 2 now.

Run the project to verify that the output is exactly as expected.

This is how static data members work.

constants in classes

Let us next come to the const keyword.

Consider this class that contains a constant member called const float RATE = 10.0f.

A developer marks a data member as constant to promise that the value of this member will not change throughout his project. Numeric and string constants are the most commonly used. Values must be given immediately, like we have done const float RATE = 10.0f.

We can add a string constant const String BANK = "Bank of XYZ"; We have immediately given it a string value. The compiler knows its value when it is compiling.

Lastly, we have added a function called Print to display the values.

A constant is accessed in the same way as a static. It is accessed by using the name of the class. So we have accessed it as MyClass.RATE.

class MyClass
  // known to the compiler 
  public const float RATE = 10.0f;

  // known to the compiler 
  public const String BANK = "Bank of XYZ";

  public void Print()
    // substitutions done by compiler 
    // constants folded away! 
    // memory allocations for consts saved thus 


    Console.WriteLine($"Rate of Interest = {MyClass.RATE}");

Now what's the whole point about the const keyword? You must be wondering about this question right now.

Notice that the compiler knows the value of a constant member at design-time. So it can substitute the value wherever the identifier has been used. This means it can fold away the constants and prevent un-necessary allocation of memory!

After substitution, the compiler compiles Rate of Interest = 10.0f; instead of Console.WriteLine(RATE); . Hence, it doesn't have to create memory for storing the constant float member.

This is how constants work. Thanks!

This Blog Post/Article "(C# Language) static and const Members" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.