Const string vs. static readonly string in c #

In C #, what's the difference between

static readonly string MyStr; 

and

 const string MyStr; 
+105
c #
Jul 6 '10 at 23:11
source share
5 answers

When you use the string const , the compiler enters the string value at compile time.
Therefore, if you use the value of const in another assembly, update the original assembly and change the value, the other assembly will not see the change until you recompile it.

A static readonly string is a normal field that is viewed at runtime. Therefore, if the field value changes in another assembly, the changes will be visible immediately after loading the assembly without recompiling.

It also means that the static readonly string can use mutable elements like Environment.UserName or DateTime.Now.ToString() . The string const can only be initialized using other constants or literals.
Alternatively, the static readonly string can be set in the static constructor; The string const can only be initialized inside the string.

Note that a static string can be changed; you should use static readonly .

+151
Jul 06 '10 at 23:29
source share

Here is a good breakdown of the pros and cons :

So, it seems that constants should be used when it is very unlikely that the value will ever change, or if no external applications / libraries will use the constant. Readonly static fields should be used when runtime is required, or if external consumers are a factor.

+39
Jul 06 '10 at 23:13
source share

Quick response:

 public const string MyStr; 

compilation constant (you can use it as a default parameter for a method parameter, for example), and it will NOT be confused if you use such technology

 public static readonly string MyStr; 

is a runtime constant, which means that it is evaluated when the application starts, and not earlier. This is why it cannot be used as the default parameter for a method (compilation error), for example. The value stored in it may be confused.

+11
Feb 23 '16 at 10:53 on
source share

OQ asked about static string vs const . Both options have different use cases (although both are considered static).

Use const only for truly constant values ​​(for example, the speed of light - but even that depends on the environment). The reason for this rigorous tutorial is that the value of const is replaced with the use of a constant in collections that reference it, that is, you may have version problems if const changes by definition (i.e., it should not be constant at the end ends). Please note that this even affects private const fields, because you may have a base and subclass in different assemblies and private fields are inherited .

Static fields are bound to the type in which they are declared. They are used to represent values ​​that should be the same for all instances of a given type. These fields can be written as many times as you like (unless specified for read-only).

If you meant static readonly vs const , then I would recommend static readonly for almost all cases, because it is a more reliable future.

+5
Jul 06 '10 at 23:51
source share

You can only change the value of a static readonly string in the constructor of a static class or initializer of a variable, while you cannot change the value of a const string anywhere.

0
Feb 12 '17 at 22:39
source share



All Articles