A Refactoring I Should Have Been Doing for Years but Forgot, AppSettings

Posted by Peter Kellner on October 18, 2010 · 2 mins read

So, It’s always annoyed me that when I want to check an AppSettings variable from app.config or web.config I’ve had to go through code like the following:

<appSettings>
<add key="AccountStorageURL" value="http://testit.blob.core.windows.net/" />
 
public string GetStorageURLOld(string username, string password)
{
var accountStorageURL = string.Empty;
if (ConfigurationManager.AppSettings["AccountStorageURL"] != null)
{
accountStorageURL = ConfigurationManager.AppSettings.Get("AccountStorageURL");
}
return accountStorageURL;
}

I should have realized a long time ago that this code is equivalent to:

public string GetStorageURL(string username, string password)
{
return ConfigurationManager.AppSettings["AccountStorageURL"] ?? string.Empty;
}

Just thought I’d share in case someone else had forgot this also.
 
HTH’s.