改变静态变量的值是不好的做法吗?

tel*_*ari 7 c# static simple-form

我有一个静态字符串变量,我需要根据HTTP协议进行更改.

更改静态字符串变量>是不好的做法

static string QuoteWebServiceUrl = CommonFunctions.ReadAppSetting("QuoteWebServiceUrl");

if(url == "https")
{
  QuoteWebServiceUrl = CommonFunctions.ReadAppSetting("QuoteWebServiceUrlSecure");
}
else
{
  QuoteWebServiceUrl = CommonFunctions.ReadAppSetting("QuoteWebServiceUrl");
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Che*_*hen 10

不可以.当然,您可以更改静态字符串变量的值.为什么你认为这是一个糟糕的实践?

  • 考虑到在多线程环境中可能造成灾难性的后果,我很惊讶这得到了如此多的支持。 (3认同)
  • @CaptainTom:我没有看到任何地方 OP 说他正在尝试同时加载设置。我们不会默认它是一个多线程程序,对吧?否则很多关于SO的答案都是不正确的。 (2认同)