Difference Between Readonly Const and Static in C#

Readonly is Runtime Constant and we can change the value of Readonly only on runtime using a Constructor. You can define Readonly using the Readonly Keyword and it is not defined inside any method directly. You can assign values to Readonly inside a constructor and only in Declaration part.You can use Static Modifier also with Readonly.You can use Readonly with Value Types and Reference Types but Delegates and Events could not be Readonly.

Const is Compile time Constant and we can not change the value of Const. You can define Const using the Const Keyword and it can be defined inside any method directly. You can assign values to Const only in Declaration part.You can not use Static Modifier with Const because bydefault Const is static.You can only use Const with built in Value Types or Reference Types that has only null values assigned to it.

Below is the Tabular Representation of the Differences in Readonly Const and Static.

Sr.No Readonly Const Static
1 Readonly Keyword is used for it Const Keyword is used for it Static Keyword is used for it
2 It is a Runtime Constant It is Compile time Constant Static Members are initialized before it accesses
3 We can change the values of Readonly Can not change the values of Const We can change the values of Static also
4 It can not be defined inside any Method It can be defined inside any Method It can be defined inside any Method
5 Can use Static modifier with Readonly Can not use Static modifier with Const bydefault it is Static Can use Static Members only in Static Class
6 Can use Value Types and Reference Types with Readonly Can use Value Types and Reference Types with Const but only null values are assigned to reference types. Can use Value Types and Reference Types with Static
6 Can change the values of Readonly in Constructor Can not change the Const value We can assign the new values into the Static Members using the Class Name.

Static Keyword is used to specify a Static member which is common to all the objects. Static Keyword can be used with Classes ,Methods ,Fields ,Properties ,Events and Constructors but it can not be used with Indexers ,Destructors and Types other than Classes. All the Members of a Static Class must be Static.Static Methods can only access Static members of same class.Static Constructors can not be parameterised.


Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.