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

1 Answer

0 votes
by

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;

Related questions

0 votes
    What is Readonly members in C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    What is the difference between const and macro?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    What is the difference between method overloading and method overriding in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is the difference between dispose() and finalize() methods in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is the difference between abstract class and interface in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is the difference between the dispose and finalize methods in C#?...
asked Mar 31, 2021 in Technology by JackTerrance
0 votes
    What is the difference between String and string in C#?...
asked Jan 15, 2021 in Technology by JackTerrance
0 votes
    What is the difference between late binding and early binding in C#?...
asked Mar 31, 2021 in Education by JackTerrance
0 votes
    What is the difference between boxing and unboxing in C#?...
asked Mar 31, 2021 in Education by JackTerrance
0 votes
    What is the difference between String and StringBuilder in C#?...
asked Mar 31, 2021 in Education by JackTerrance
0 votes
    I'm getting the following error when trying to compile my code on Angular 7.2.0 with TypeScript version ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I'm getting the following error when trying to compile my code on Angular 7.2.0 with TypeScript version ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I'm relatively new to C++, recently moved from C# and Java (and before that, used to work in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Take the following code snippet: #include std::vector good; //illegal, because std::allocator is ill-formed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Is the following code supposed to compile? #include void foo() { const std::pair x = {1, 2}; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
...