Difference between const and readonly in C# - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Difference between const and readonly in C#

NickFNickF USAPosts: 132Member

Can someone please explain the difference between const and readonly in C#?

Tagged:

Comments

  • DavidMDavidM USAPosts: 342Member
  • mohitkumarmohitkumar indiaPosts: 14Member

    Constants In C# we can declare the constant by using keyword constant. In C# constant will be treated as compile time constant. 1. Once data is assigned to the constant it can’t be changed. 2. The constant value should be assigned at the time of compilation time only. 3. We can assign as expression to the constant but the expression value should be evaluated at the of time compilation by the compiler. 4. Constant type expression can also include another constant also but expression should not contain any instant or static variable. 5. Only one copy of constant will be created for multiple objects or same class. 6. Constants can be marked as public, private, protected, internal, or protected internal. These access modifiers define how users of the class can access the constant.

    ReadOnly Variable A read only variable is runtime constant We can assign the data to read only variables directly while declaring the variable or we can assign the data to read only variable in constructor also.

    For learning more about c# interview questions

Sign In or Register to comment.