Home » Tutorials » C# » Constant vs ReadOnly in C#

Constant vs ReadOnly in C#

Difference between Var and Dynamic in c#
Export to Excel using Open XML

Constant vs Readonly in C#

Constant and Readonly keywords are used to assign fix value to variable through out the application, which could not be changed.

Constant in C#

Constant field or variable must be assigned at declaration time and after that it would not be changed or modified. By default constant is static, so constant would not be defined as Static.

Constant field or variable is also known as  immutable value, which is know at compile time and couldn’t change in runtime.

Example 1: 

Example 2:

Example 3:

Readonly in C#

Readonly field or variable could be assigned at runtime, once it will be assigned could not be changed through out the application.

Example 1:

Readonly keyword can be applied to value type and reference type (which initialized by using the new keyword) both. Note, delegate and event could not be readonly.

About

Hello friends, My self Dhruv Sheth, I have more than 6 years of Industrial experience as a Software Developer. In my career I got chance to work with asp.net, c#,Web API, JavaScript, JQuery, WPF, WCF , Windows Application, PostScript, vb.net, GhostScript etc. So I have decided to share my knowledge with everyone and finally I have come up with TechnoThirsty.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload CAPTCHA.




DISCLAIMER:
The content is copyrighted to technothirsty.com and may not be reproduced on other websites without permission from the owner.You may contact us using the information below.