Resharper always suggesting me to make const string instead of string

后端 未结 4 2442
挽巷
挽巷 2021-02-20 13:38

which one is good:

string sQuery = \"SELECT * FROM table\";

or

const string sQuery = \"SELECT * FROM table\";
<
4条回答
  •  爱一瞬间的悲伤
    2021-02-20 14:14

    If the string never changes and is never used outside your assembly, then const is a good idea. If it never changes but is used outside your assembly, static readonly might be a better idea -- consts are "burned in" at the site of the call, not stored in one location, so recompiling the assembly that contains the const does not update the dependent assemblies -- they have to be recompiled too. static readonly variables on the other hand do get updated in dependent assemblies.

提交回复
热议问题