Thursday 24 August 2017

Constant vs Readonly vs Static Keywords in C#

This article explains the three most commonly used but confusing keywords. You can use these keywords in your code correspondingly but what you need to learn about these keywords is, how they are function differently, their behaviour and their different operation. I am explaning with simple snippets by taking reference of 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 are all 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;  
  5. }  
Output: 11

Explanation

The preceding code snippet will produce a result of 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;  
  6. }  
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).

For that reason a 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.    }  
  9. }  
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 must 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.     }  
  8. }  
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 happens then.
  1. class ReadOnly  
  2. {  
  3.     int i = 9;  
  4.     public static void disp()  
  5.     {  
  6.         Console.WriteLine(i);  
  7.     }  
  8. }  
Explanation

This snippet will show an 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

No comments:

Post a Comment