Difference b/w Const, Readonly and Static keywords !

Constant vs Readonly vs Static in CSharp

Const

Definition from MSDN : ‘Constants are immutable values which are known at compile time and do not change their values for the life of the program’.

In simple terms, a Constant is a variable of which the value is made constant at compile time. Constants are static by default and it is mandatory to assign a value to it. The value of a Constant cannot be changed throughout the entire program. It is often called as Compile-Time Constant.

Examples:

– A Constant should be assigned a value at the time of declaration:

c1

– The value of a Constant cannot be modified throughout the life of program. In the below example I tried to modify the value in a default constructor, static constructor, parameterized constructor and a simple method, all of them give compile time error.

c2

Readonly

Definition from MSDN : ‘Readonly variables are also immutable values which are known at run time and do not change their values for the life of the program’.

Readonly is a variable whose value can be assigned or changed during runtime but only through the non-static constructor, not even a method. It is not mandatory to assign a value to Readonly variable at the time of declaration as it can be assigned at runtime as well. It is often called as Run-Time Constant.

Examples:

– A Readonly field can be assigned a value at the time of declaration or in a non-static constructor only. In the below example when I try to assign a value to ReadOnly field in a static constructor or a simple method it gives compile time error.

r1

We can assign or change the value of a Readonly field in a default or parameterized constructor of the same class. The below example gives no compile time or runtime error.

r2

Also, explicitly we can specify a Readonly field as static since by default it is not static. In that case the value can be assigned at either declaration time or in a static constructor only and cannot be modified further.

Static

Definition from MSDN : ‘Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier 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’.

Whenever we write a function or declare a variable, it doesn’t create instance in a memory until we create object of class. But if we declare them with static modifier, it directly creates instance in memory and acts globally.

Example :

– To understand the difference b/w a static and non-static data member in the below example we have two variables- (x) as static and (y) as non-static. In the main method, we can directly access the static variable (x) using the class name i.e no object reference of the class is required. We also created two objects of SimpleClass and displayed the values in default constructor. The value of non-static member (y) gets reinitialized every time a new instance of the class is created while the value of static member (x) is shared by all instances of the class.

s2

Hope this makes the difference and the usage of the three keywords a bit clear.

Please leave your comments and feedback.

Thanks for checking out !

References- MSDN, C-sharpcorner.com, dotnet-tricks.com

Categories
Comments
All comments.
Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.