View on GitHub qJake's

Tidbits

Bits of development knowledge from experience and headache.

AppSettings Return Values using ConfigurationManager

Posted on: Monday, December 21, 2015

ConfigurationManager uses the following rules to determine what to return when indexing into the AppSettings collection:

  1. If there is a value present, return it as a string.
  2. If there is no value present (empty string), return an empty string.
  3. If there is no value attribute defined, return an empty string.
  4. If there is no matching key defined, return null.

Example

Given these settings:

<appSettings>
  <add key="Key1" value="abc123" />
  <add key="Key2" value="" />
  <add key="Key3" />
</appSettings>

And the following code:

Console.WriteLine($"[{ConfigurationManager.AppSettings["Key1"] ?? "NULL"}]");
Console.WriteLine($"[{ConfigurationManager.AppSettings["Key2"] ?? "NULL"}]");
Console.WriteLine($"[{ConfigurationManager.AppSettings["Key3"] ?? "NULL"}]");
Console.WriteLine($"[{ConfigurationManager.AppSettings["Key4"] ?? "NULL"}]");

The following output is produced:

[abc123]
[]
[]
[NULL]