Const vs. readonly

Today I found an article where the const field is called the compile-time constant, and the readonly field is called the run-time constant. Two phrases taken from Effective C #. I searched the MSDN and language specifications, found nothing about the runtime constant.

Not offensive, but I don't think the runtime constant is the right phrase.

 private readonly string foo = "bar"; 

creates a variable called "foo" whose value is "bar", and the value is read-only, here it is a variable that has nothing to do with constant . The readonly variable is still a variable; it cannot be a constant. Variables and constants are mutually exclusive.

Perhaps this question goes overboard, but I want to listen to the opinions of others. What do you think?

+8
source share
5 answers

As you yourself have noted, this term is not used in the language specification, etc. So; blame this book! I would call this field โ€œreadonlyโ€ because it is what it is where the definition of โ€œreadonlyโ€ here refers to the initializer / constructor and is limited to regular code . For example, even readonly fields can change ...

 // how to annoy your colleagues... typeof(string).GetField("Empty").SetValue(null, " "); 

(Note that this no longer works in recent versions of the CLR - JIT seems to replace the field load with ldstr - but it really is a very long time)

(more real reasons for doing this on objects relate to deserialization)

+6
source

I believe that the author means the following:

Consider an example:

 public class A { public const int a = Compute(); private static int Compute(){ /*some computation and return*/ return some_computed_value; } } 

will not compile since you must have a constant value for assignment a . Therefore, this value is a compile-time constant.

Instead, if you change this to

 public class A { public readonly int a = Compute(); private static int Compute(){ /*some computation and return*/ return some_computed_value; } } 

this one will be compiled. It computes and assigns it a at runtime. This value is a run-time constant.

+10
source

The readonly variable can only be changed in its constructor and can be used for complex objects. A constant variable cannot be changed at run time, but can only be used for simple types such as Int, Double, String. The run-time constant is somewhat accurate, but confusing the problem, there are very obvious differences between constant and readonly, so naming one that looks like the other is probably not a good idea, although they are often used for the same purpose.

A brief description of the differences is here.

+2
source

I would call readonly the variable "write once" , which is checked by the compiler, and not at run time. You can write a field using reflection, so it is not constant at run time.

+2
source

const vs readonly only

This is a complete summary comparison between const and readonly :

 |-----------------------------------------|-----------------------------------------| | Constant Field | Read-only Field | |-----------------------------------------|-----------------------------------------| | Compile-time constant | Run-time constant | |-----------------------------------------|-----------------------------------------| | Assigned to an expression evaluated at | Assigned to any valid expression at | | compile time | runtime | |-----------------------------------------|-----------------------------------------| | Assigned on declaration | Assigned on declaration or constructor | |-----------------------------------------|-----------------------------------------| | Only number, Boolean or string | Any data type | |-----------------------------------------|-----------------------------------------| | Always static | Optionally static | |-----------------------------------------|-----------------------------------------| 

explanation

  • Constants are static (there is no need to instantiate a class to access them).
  • Constants can only be assigned upon declaration.
  • Constants are compile-time constants, because they are assigned to the expression computed at compile time: when they change, the project must be recompiled to use the new values.
  • Read-only fields are variables in the class that contain a value that is initialized (only in the constructor or in the declaration) and then not changed.
  • Read-only fields are a run-time constant, as they can be assigned to any valid expression at run time.
  • The read-only field applies to the instance of the object, but not to the property of that instance. Thus, other code may change the read-only property of the field instance.
  • Read-only fields can be static (if you want to make them common to all instances).

When to use const and when to use only for readonly ?

  • Constants are useful when they are of a simple type and their values โ€‹โ€‹will never be changed.
  • Read-only fields are useful when they are initialized from a source (file, database or other codes, etc.), but then they will not be changed.
0
source

All Articles