138

Will ConfigurationManager.AppSettings["blah"] throw an exception if "blah" doesn't exist in the web/app.config?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ben Aston
  • 53,718
  • 65
  • 205
  • 331

7 Answers7

142

No, it returns null.

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
26

From the MSDN documentation for NameValueCollection.Item Property (String):

Caution

This property returns null in the following cases: 1) if the specified key is not found; and 2) if the specified key is found and its associated value is null. This property does not distinguish between the two cases.

Community
  • 1
  • 1
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
7

No, it returns null.

AppSettings is a NameValueCollection - as per the caution on the NameValueCollection.Get page:

This method returns a null reference (Nothing in Visual Basic) in the following cases: 1) if the specified key is not found; and 2) if the specified key is found and its associated value is a null reference (Nothing in Visual Basic). This method does not distinguish between the two cases.

Dexter
  • 18,213
  • 4
  • 44
  • 54
6

No, it returns null.

ConfigurationManager.AppSettings is a NameValueCollection - from the MSDN documentation:

The Get method does not distinguish between null which is returned because the specified key is not found and null which is returned because the value associated with the key is null.

(my emphasis)

Andrew
  • 12,991
  • 15
  • 55
  • 85
3

Other answers reference the documentation for the Item property. It might not be immediately obvious why they are relevant looking at the following code snippet.

ConfigurationManager.AppSettings["blah"]

The square bracket syntax is used in C# to access indexers. These are special properties that allow a class to be indexed in the same way that an array can be. Looking at the definition of the NameValueCollection.Item property, you will notice that it does not use the normal property syntax. The this keyword and the indexer parameters are used to define this property as an indexer.

public string this[
    string name
] { get; set; }

In the documentation, indexers are implicitly named Item and parameters are surrounded by square brackets.

Indexers as shown in the MSDN documentation.

It is not clear to me why there were answers that referenced the Get method - maybe one calls the other?

At any rate, to answer the question...

No. An exception will not be thrown if you access a non-existent key - a null will be returned.

Here is the relevant section from the NameValueCollection.Item property documentation.

This property returns null in the following cases: 1) if the specified key is not found; and 2) if the specified key is found and its associated value is null. This property does not distinguish between the two cases.

Scott Munro
  • 13,369
  • 3
  • 74
  • 80
1

As Tim said, it will just return null.

However, if you want it to throw an exception when not found one could do this:

var myImportantSetting= ConfigurationManager.AppSettings["important_setting"] ?? throw new SettingsPropertyNotFoundException("AppSetting missing.");
Kasper
  • 21
  • 4
-2

Yes http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

Edit: this is clearly wrong. Left for the helpful comments below.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 1
    the answer to your question is No like @Tim mentioned. The page pointed to just said that if appsettings are not able to be loaded then an exception is thrown. But if a value is just not present in the appsettings then you will not get an exception. It really wouldn't make sense to throw an error just because a value doesn't exist in a dictionary. But if the dictionary didn't exist then that would be a reason to throw an error. (The term dictionary was just used to refer to an arbitrary collection.) – spinon Jul 06 '10 at 14:51
  • That link says that an exception is thrown if the `NameValueCollection` could not be found - i.e. the system could not find *any* settings. Name Value Collections do not thrown an exception when you try to retrieve a single value that does not exist.. – Dexter Jul 06 '10 at 14:51