.net 如果“blah”不存在,ConfigurationManager.AppSettings["blah"] 会抛出异常吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3187236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Will ConfigurationManager.AppSettings["blah"] throw an exception if "blah" doesn't exist?
提问by Ben Aston
Will ConfigurationManager.AppSettings["blah"] throw an exception if "blah" doesn't exist in the web/app.config?
如果 web/app.config 中不存在“blah”,ConfigurationManager.AppSettings["blah"] 会抛出异常吗?
Sincere apologies for the super lazy question.
对超级懒惰的问题表示诚挚的歉意。
回答by Tim Robinson
No, it returns null.
不,它返回null。
回答by Martin Liversage
From the MSDN documentation for NameValueCollection.Item Property (String):
从NameValueCollection.Item Property (String)的MSDN 文档中:
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.
警告
此属性在以下情况下返回 null: 1) 如果未找到指定的键;和 2) 如果找到指定的键并且其关联值为空。此属性不区分这两种情况。
回答by Dexter
No, it returns null.
不,它返回null。
AppSettings is a NameValueCollection- as per the cautionon the NameValueCollection.Getpage:
AppSettings 是NameValueCollection- 根据NameValueCollection.Get页面上的警告:
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.
在以下情况下,此方法返回空引用(在 Visual Basic 中为 Nothing): 1) 如果未找到指定的键;和 2) 如果找到指定的键并且其关联值是空引用(在 Visual Basic 中为 Nothing)。这种方法不区分这两种情况。
回答by Andrew
No, it returns null.
不,它返回null。
ConfigurationManager.AppSettingsis a NameValueCollection- from the MSDN documentation:
ConfigurationManager.AppSettings是NameValueCollection- 来自MSDN 文档:
The Get method does not distinguish between null which is returned because the specified key is not foundand null which is returned because the value associated with the key is null.
Get 方法不区分因未找到指定键而返回的 null 和因与该键关联的值为 null 而返回的 null。
(my emphasis)
(我的重点)
回答by Scott Munro
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.
其他答案参考了 Item 属性的文档。查看以下代码片段可能不会立即明白为什么它们是相关的。
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.Itemproperty, 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.
C# 中使用方括号语法来访问索引器。这些是允许类以与数组相同的方式进行索引的特殊属性。查看NameValueCollection.Item属性的定义,您会注意到它没有使用正常的属性语法。this 关键字和索引器参数用于将此属性定义为索引器。
public string this[
string name
] { get; set; }
In the documentation, indexers are implicitly named Item and parameters are surrounded by square brackets.
在文档中,索引器被隐式命名为 Item,参数用方括号括起来。


It is not clear to me why there were answers that referenced the Getmethod - maybe one calls the other?
我不清楚为什么有引用Get方法的答案- 也许一个调用另一个?
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.Itemproperty documentation.
这是NameValueCollection.Item属性文档中的相关部分。
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.
此属性在以下情况下返回 null: 1) 如果未找到指定的键;和 2) 如果找到指定的键并且其关联值为空。此属性不区分这两种情况。
回答by Ben Aston
Yes http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
是http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
Edit: this is clearly wrong. Left for the helpful comments below.
编辑:这显然是错误的。留给下面的有用评论。

