ConfigurationManager operation doubt

  • 911
  • 0
  • C#
  • 2013-06-15

摘要:ConfigurationManager operation doubt

 

我們常常會在 web.config設定一些參數,

 

並且使用ConfigurationManager.Appsettings("參數")來讀取預設的參數,

 

而ConfigurationManager也是一個static class,
 
 
表示它會被預先載入記憶體,當然包含設定在Config裡面的所有屬性。
 
 
但是有時我們在程式裡面為了寫的比較精簡有可能會這樣寫,
 
 

string temp = ConfigurationManager.Appsettings("參數");

string params = string.format("http://www.yahoo.com.tw?{0}{1}","test",temp);
 
 
可是這樣我們等於宣告了另外一個參數(temp)來去做承接,
 
 
若我們使用如下:
 

string params = string.format("http://www.yahoo.com.tw?{0}{1}","test",ConfigurationManager.Appsettings("參數"));
 
 
我們減少宣告temp參數,是不是等於減少記憶體的使用了?
 
其實不是的,當我們使用temp參數來取得ConfigurationManger要讀取的參數時,
 
這時候在 stack會新增一個參考位址,
 
並且指向早已儲存在記憶體的參數而已,
 
因此這樣的用法其實並不會多配置記憶體。