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

前端 未结 30 2662
挽巷
挽巷 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:20

    A constant will be compiled into the consumer as a literal value while the static string will serve as a reference to the value defined.

    As an exercise, try creating an external library and consume it in a console application, then alter the values in the library and recompile it (without recompiling the consumer program), drop the DLL into the directory and run the EXE manually, you should find that the constant string does not change.

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

    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'll update I_CONST_VALUE to 20 in the future. 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. 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 coz he pointed this out first. Also I need to plug where I learned this.. Effective C# - Bill Wagner

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

    Read Only : Value can be changed through Ctor at runtime. But not through member Function

    Constant : By default static. Value cannot be changed from anywhere ( Ctor, Function, runtime etc no-where)

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

    ReadOnly :The value will be initialized only once from the constructor of the class.
    const: can be initialized in any function but only once

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

    Const: Absolute constant value during the application life time.

    Readonly: It can be changed in running time.

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

    Constants

    • Constants are static by default
    • They must have a value at compilation-time (you can have e.g. 3.14 * 2, but cannot call methods)
    • Could be declared within functions
    • Are copied into every assembly that uses them (every assembly gets a local copy of values)
    • Can be used in attributes

    Readonly instance fields

    • Must have set value, by the time constructor exits
    • Are evaluated when instance is created

    Static readonly fields

    • Are evaluated when code execution hits class reference (when new instance is created or a static method is executed)
    • Must have an evaluated value by the time the static constructor is done
    • It's not recommended to put ThreadStaticAttribute on these (static constructors will be executed in one thread only and will set the value for its thread; all other threads will have this value uninitialized)
    0 讨论(0)
提交回复
热议问题