'const string' vs. 'static readonly string' in C#

前端 未结 5 1943
臣服心动
臣服心动 2020-12-02 06:39

In C#, what\'s the difference between

static readonly string MyStr;

and

const string MyStr;

?

5条回答
  •  一整个雨季
    2020-12-02 07:05

    Here is a good breakdown of the pros and cons:

    So, it appears that constants should be used when it is very unlikely that the value will ever change, or if no external apps/libs will be using the constant. Static readonly fields should be used when run-time calculation is required, or if external consumers are a factor.

提交回复
热议问题