Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Difference between Constant and Readonly in C#

What is a Readonly Keyword?

The keyword Readonly has a value that can be altered or allocated at runtime, but only through the non-static constructor.

What is a Constant Keyword?

Constant fields and constant local are declared with the const keyword in C#. The constant field’s value remains constant throughout the programme. When it comes to C#, both locals and constant fields are not variables; a constant field is a string, number, boolean values, and null reference.

Difference between Constant and Readonly in C#

S.No. Readonly Keyword Const Keyword
1 In C#, we can use the readonly keyword to create readonly fields. In C#, we can use the const keyword to create constant fields.
2 It belongs to runtime. It belongs to compile-time.
3 The readonly field’s value can be altered. The const field’s value cannot be modified.
4 It is not possible to declare it inside the method. It is possible to declare it inside the method.
5 In the case of readonly fields, we can allocate values in the construction part and declaration segment. In the case of const fields, we can only allocate values in the declaration segment.
6 We can use it with static modifiers. We cannot use it with static modifiers.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Application FormGATE SyllabusGATE Cut offGATE Previous Year Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*