Constant vs Readonly vs Static in C#

This article explains the three most commonly used but confusing keywords. I am explaining them in reference to C#.

Agenda

  • Overview
  • Constant

    • Snippet
    • Explanation
    • Points to Remember

  • Readonly

    • Snippet
    • Explanation
    • Points to Remember

  • Static

    • Snippet
    • Explanation
    • Points to Remember

  • Conclusion

Overview

Constant, readonly and static are keywords that are nearly the same in behavior but when we talk about their functioning they all are different. I'll explain all three wirth some examples, codes and their respective examples.

This article will also include some key points that you need to remember, so that you can easily differentiate among these three.

Constant vs Readonly vs Static in CSharp

Constant

Constant fields are defined at the time of declaration in the code snippet, because once they are defined they can't be modified. By default a constant is static, so you can't define them static from your side.

It is also mandatory to assign a value to them at the time of declaration otherwise it will give an error during compilation of the program snippet. That's why it is also called a compile-time constant.

Snippet

  1. void Sum(int j)  
  2. {  
  3. const int i = 9, k = 2;  
  4. const int A = i + k;  

Output: 11

Explanation

The preceding code snippet will produce a result 11, without showing any error since we already declared it at the initial point of declaration.

What if, we make some changes in the code above, such as:

  1. void Sum(int j)  
  2. {  
  3.    const int i = 9, k = 2;  
  4. // const int A = i + k;  
  5.    Const int B = i + j;  

Explanation

This code snippet will take you toward a compile time error, because there is no initialization, since it's evaluated at run time.

Points to Remember

  • Compile time constant
  • Can't be declared static
  • Can't be modified or changed
  • Can be of any type of Access Modifier
  • Local scope
  • Needs to get initialized
  • Declared at the time of declaration

Readonly

A Readonly field can be initialized either at the time of declaration or within the constructor of the same class. We can also change the value of a Readonly at runtime or assign a value to it at runtime (but in a non-static constructor only).

Due to that reason Readonly field is also called a run-time constant.

Snippet

  1. class ReadOnly  
  2. {  
  3.    readonly int i;  
  4.    public ReadOnly( )  
  5.    {  
  6.        i = 11;  
  7.        Console.WriteLine(i);  
  8.    }  

Explanation

We can assign the value to an integer later in the snippet; this is possible when using the Readonly keyword. We can modify it too depending on the use.

Points to Remember

  • Run-time constant
  • It can be static
  • Global scope
  • Can be declared in the constructer class
  • Generally public

Static

The static keyword is used to declare a static member. If we are declare a class as a static class then in this case all the class members needs to be static too. The static keyword can be used effectively with classes, fields, operators, events, methods and so on effectively.

Snippet

  1. class ReadOnly  
  2. {  
  3.     static int I = 11;  
  4.     public static void disp()  
  5.     {  
  6.         Console.WriteLine(i);  
  7.     }  

Output: 11

Explanation

This code will show no error and produce a result (11), since we declared its value to be static at the time of declaration. So we can access it depending on our use in the program.

Let's make some changes in the snippet and see what happen then.

  1. class ReadOnly  
  2. {  
  3.     int I = 9;  
  4.     public static void disp()  
  5.     {  
  6.         Console.WriteLine(i);  
  7.     }  

Explanation

This snippet will show a error, because we didn't declare a value for the static and we are trying to access it within a method. We can't do that.

Points to Remember

  • Can't be used with indexers
  • Works with constructors too
  • By default it is private
  • Can be parameterized or public too
  • If its applied to a class then all the class members need to be static

Conclusion

I hope now you will have 3 different states in your mind regarding constant, static and readonly keywords and that you will be able to differentiate among them easily.

Up Next
    Ebook Download
    View all
    Learn
    View all