Resharper always suggesting me to make const string instead of string

后端 未结 4 2407
挽巷
挽巷 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:29

    It does this because if you accidentally assign a new value to sQuery in your code, if it's a const you'll get a compile error, so it will catch a bug at compile time. Same with its suggestion to make member variables which are set in the ctor only to be readonly

提交回复
热议问题