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 7415003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:12:46+00:00 2026-05-29T07:12:46+00:00

There are many questions about this subject , but none (except one but still

  • 0

There are many questions about this subject , but none (except one but still a short one) are dealing with the following scenario.

From C# 4 book:

enter image description here

Marc also wrote :

if you change the value of a const, you need to rebuild all the
clients

Question :

1) Why is that? Are both static readonly and const— static?

2) Where actually the values are saved ?

3) How does making a field static readonly actually solve this problem “behind the scene” ?

  • 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. Editorial Team
    Editorial Team
    2026-05-29T07:12:48+00:00Added an answer on May 29, 2026 at 7:12 am

    no, a const is a const, not a static – it is a special-case, with different rules; it is only set at compile-time (not runtime), and it is handled differently

    the crux here is what the following means:

    var foo = SomeType.StaticValue;
    

    vs

    var bar = SomeType.ConstValue;
    

    in the first case, it reads the value at runtime from SomeType, i.e. via a ldsfld; however, in the second case, that is compiled to the value, i.e. if ConstValue happens to be 123, then the second is identical to:

    var bar = 123;
    

    at runtime, the fact that it came from SomeType does not exist, as the value (123) was evaluated by the compiler, and stored. Hence it needs a rebuild to pick up new values.

    Changing to static readonly means that the “load the value from SomeType” is preserved.

    So the following:

    static int Foo()
    {
        return Test.Foo;
    }
    static int Bar()
    {
        return Test.Bar;
    }
    ...
    static class Test
    {
        public static readonly int Foo = 123;
        public const int Bar = 456;
    }
    

    compiles as:

    .method private hidebysig static int32 Bar() cil managed
    {
        .maxstack 8
        L_0000: ldc.i4 0x1c8
        L_0005: ret 
    }
    
    .method private hidebysig static int32 Foo() cil managed
    {
        .maxstack 8
        L_0000: ldsfld int32 ConsoleApplication2.Test::Foo
        L_0005: ret 
    }
    

    Note that in the Bar, the ldc is loading a value directly (0x1c8 == 456), with Test completely gone.

    For completeness, the const is implemented with a static field, but – it is a literal field, meaning: evaluated at the compiler, not at runtime.

    .field public static literal int32 Bar = int32(0x1c8)
    .field public static initonly int32 Foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seem to be many questions asked about this subject here on stackoverflow, but
I know there are many questions about this but none of them helped my
NOTE: I know there are many questions that talked about that but I'm still
There are many questions like this, but none of them seem to answer my
There are already so many questions asked about this already. One popular answer is
I know there are many questions and answers about this, but I am looking
ok I am well aware there are many other questions about this, but I
I know there exists many questions about this, but I dont know what to
I know there is many questions about CSS inheritance but none of what i
There are many questions about this PersistenceException, but I have not seen some, where

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.