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

前端 未结 30 2672
挽巷
挽巷 2020-11-22 05:05

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

When would you use one over the other?

相关标签:
30条回答
  • 2020-11-22 05:34
    • when to use const or readonly

      • const

        • compile-time constant: absolute constant, value is set during declaration, is in the IL code itself
      • readonly

        • run-time constant: can be set in the constructor/init via config file i.e. App.config, but once it initializes it can't be changed
    0 讨论(0)
  • 2020-11-22 05:34

    One thing to add to what people have said above. If you have an assembly containing a readonly value (e.g. readonly MaxFooCount = 4; ), you can change the value that calling assemblies see by shipping a new version of that assembly with a different value (e.g. readonly MaxFooCount = 5;)

    But with a const, it would be folded into the caller's code when the caller was compiled.

    If you've reached this level of C# proficiency, you are ready for Bill Wagner's book, Effective C#: 50 Specific Ways to Improve Your C# Which answers this question in detail, (and 49 other things).

    0 讨论(0)
  • 2020-11-22 05:36

    The key difference is that Const is the C equivalent of #DEFINE. The number literally gets substituted a-la precompiler. Readonly is actually treated as a variable.

    This distinction is especially relevant when you have Project A depending on a Public constant from Project B. Suppose the public constant changes. Now your choice of const/readonly will impact the behavior on project A:

    Const: project A does not catch the new value (unless it is recompiled with the new const, of course) because it was compiled with the constants subtituted in.

    ReadOnly: Project A will always ask project B for it's variable value, so it will pick up the new value of the public constant in B.

    Honestly, I would recommend you use readonly for nearly everything except truly universal constants ( e.g. Pi, Inches_To_Centimeters). For anything that could possibly change, I say use readonly.

    Hope this helps, Alan.

    0 讨论(0)
  • 2020-11-22 05:37

    The difference is that the value of a static readonly field is set at run time, so it can have a different value for different executions of the program. However, the value of a const field is set to a compile time constant.

    Remember: For reference types, in both cases (static and instance), the readonly modifier only prevents you from assigning a new reference to the field. It specifically does not make immutable the object pointed to by the reference.

    For details, please refer to C# Frequently Asked Questions on this topic: http://blogs.msdn.com/csharpfaq/archive/2004/12/03/274791.aspx

    0 讨论(0)
  • 2020-11-22 05:38

    A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared.

    public class MyClass
    {
        public const double PI1 = 3.14159;
    }
    

    A readonly member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor, as well being able to be initialized as they are declared.

    public class MyClass1
    {
         public readonly double PI2 = 3.14159;
    
         //or
    
         public readonly double PI3;
    
         public MyClass2()
         {
             PI3 = 3.14159;
         }
    }
    

    const

    • They can not be declared as static (they are implicitly static)
    • The value of constant is evaluated at compile time
    • constants are initialized at declaration only

    readonly

    • They can be either instance-level or static
    • The value is evaluated at run time
    • readonly can be initialized in declaration or by code in the constructor
    0 讨论(0)
  • 2020-11-22 05:38

    One of the team members in our office provided the following guidance on when to use const, static, and readonly:

    • Use const when you have a variable of a type you can know at runtime (string literal, int, double, enums,...) that you want all instances or consumers of a class to have access to where the value should not change.
    • Use static when you have data that you want all instances or consumers of a class to have access to where the value can change.
    • Use static readonly when you have a variable of a type that you cannot know at runtime (objects) that you want all instances or consumers of a class to have access to where the value should not change.
    • Use readonly when you have an instance level variable you will know at the time of object creation that should not change.

    One final note: a const field is static, but the inverse is not true.

    0 讨论(0)
提交回复
热议问题