Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 47299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:06:03+00:00 2026-05-10T16:06:03+00:00

What is the difference between const and readonly in C#? When would you use

  • 0

What is the difference between const and readonly in C#?

When would you use one over the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:06:04+00:00Added an answer on May 10, 2026 at 4:06 pm

    Apart from the apparent difference of

    • having to declare the value at the time of a definition for a const VS readonly values can be computed dynamically but need to be assigned before the constructor exits. After that it is frozen.
    • const‘s are implicitly static. You use a ClassName.ConstantName notation to access them.

    There is a subtle difference. Consider a class defined in AssemblyA.

    public class Const_V_Readonly {   public const int I_CONST_VALUE = 2;   public readonly int I_RO_VALUE;   public Const_V_Readonly()   {      I_RO_VALUE = 3;   } } 

    AssemblyB references AssemblyA and uses these values in code. When this is compiled:

    • in the case of the const value, it is like a find-replace. The value 2 is ‘baked into’ the AssemblyB‘s IL. This means that if tomorrow I update I_CONST_VALUE to 20, AssemblyB would still have 2 till I recompile it.
    • in the case of the readonly value, it is like a ref to a memory location. The value is not baked into AssemblyB‘s IL. This means that if the memory location is updated, AssemblyB gets the new value without recompilation. So if I_RO_VALUE is updated to 30, you only need to build AssemblyA and all clients do not need to be recompiled.

    So if you are confident that the value of the constant won’t change, use a const.

    public const int CM_IN_A_METER = 100; 

    But if you have a constant that may change (e.g. w.r.t. precision) or when in doubt, use a readonly.

    public readonly float PI = 3.14; 

    Update: Aku needs to get a mention because he pointed this out first. Also I need to plug where I learned this: Effective C# – Bill Wagner

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seems OK,are the priviledges set correctly? May 12, 2026 at 4:10 pm
  • Editorial Team
    Editorial Team added an answer With visual studio you can use the Generate Preprocessed File… May 12, 2026 at 4:10 pm
  • Editorial Team
    Editorial Team added an answer a.) I believe the answer is yes, at the mysql… May 12, 2026 at 4:10 pm

Related Questions

What is the difference between a const_iterator and an iterator and where would you
Is there any difference between pointer to const and usual pointer for functions? When
What is the difference between void func(const Class *myClass) and void func(Class *const myClass)
Question: What is the difference between: vector<string> and vector<char *> ? How would I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.