User Name:


User Email:




This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)
  

crazydeveloper Constant and ReadOnly and Static Variables in C#

These are very common keywords use in C# and are quite confusing. So today we will discuss these keywords and try to understand them. Constant, Readonly and Static are keywords that are nearly the same in behavior but when we talk about their functioning they Constant vs Readonly vs Static in CSharpare all different.

Constant

Constant fields or local variables must be assigned a value at the time of declaration and after that they cannot be modified. By default constant are static, hence you cannot define a constant type as static.


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.


Look at the following code:

  1. public const int X = 10;
  2. void Calculate(int Z)
  3. {
  4. const int X = 10, X1 = 50;
  5. const int Y = X + X1; //no error, since its evaluated at compile time
  6. const int Y1 = X + Z; //gives error, since its evaluated at run time
  7. }
  8. const MyClass obj1 = null;//no error, since its evaluated at compile time
  9. const MyClass obj2 = new MyClass();//gives error, since its evaluated at run time


Constants can be marked as public, private, protected, internal, or protected internal access modifiers. Use the const modifier when you sure that the value a field or local variable would not be changed.

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 same class.  Readonly field is also called a run-time constant.

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).


Look at the following code:

  1. class MyClass
  2. {
  3. readonly int X = 10; // initialized at the time of declaration
  4. readonly int X1;
  5. public MyClass(int x1)
  6. {
  7. X1 = x1; // initialized at run time
  8. }
  9. }


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 specify a static member, which means static members are common to all the objects and they do not tied to a specific object. This keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.


Look at the following code:

  1. static int X = 10;


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



Happy reading!Smile

Abhishek


crazydeveloper Home Page 17 June 2015

Become a Fan