简体   繁体   English

DictionarySectionHandler和NameValueSectionHandler之间有区别吗?

[英]Is there a difference between DictionarySectionHandler and NameValueSectionHandler?

In .NET, we can create custom configuration sections using the <configSections> element, like so: 在.NET中,我们可以使用<configSections>元素创建自定义配置节,如下所示:

<configuration>
  <configSections>
    <section name="dictionarySample"
             type="System.Configuration.DictionarySectionHandler"/>
    <section name="nameValueSample"
             type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <dictionarySample>
    <add key="key1" 
         value="value1"/>
  </dictionarySample>
  <nameValueSample>
    <add key="key2" 
         value="value2" />
  </nameValueSample>
</configuration>

Above, I am defining two sections. 上面,我定义了两个部分。 One of type DictionarySectionHandler , and another of type NameValueSectionHandler . 一个是DictionarySectionHandler类型,另一个是NameValueSectionHandler类型。

As far as I can tell, these two Handlers are used in exactly the same way and result in identical configuration sections. 据我所知,这两个处理程序以完全相同的方式使用,并导致相同的配置部分。

So, is there a difference, or can I use them interchangeably? 那么,是否存在差异,或者我可以互换使用它们吗?

TL;DR NameValueSectionHandler is fine for string -> string pairs in simple situations, but if you need your configuration to be efficient (particularly if you're going to be using remove repeatedly), use DictionarySectionHandler . TL; DR NameValueSectionHandler适用于简单情况下的string - > string对,但如果您需要配置有效(特别是如果您要反复使用remove ),请使用DictionarySectionHandler


I dug into the source of these two classes ( NameValue , Dictionary ), and found very little difference in implementation. 我挖掘了这两个类的来源( NameValueDictionary ),发现实现上差别很小。

There are two things worth noting, though: 但有两件事值得注意:

  1. As the names of the handlers hint at, the primary difference is in the collections they use: DictionarySectionHandler stores its key/value pairs in a Hashtable , whereas NameValueSectionHandler uses a NameValueCollection . 正如处理程序的名称所暗示的那样,主要区别在于它们使用的集合: DictionarySectionHandler将其键/值对存储在Hashtable ,而NameValueSectionHandler使用NameValueCollection
  2. In the DictionarySectionHandler , the value is not required and will default to an empty string if it is not provided, but NameValueSectionHandler requires the value . DictionarySectionHandler ,该value不是必需的,如果未提供,则默认为空字符串,但NameValueSectionHandler需要该value

As far as the differences between Hashtable and NameValueCollection , NameValueCollection can have duplicate keys, but Hashtable cannot. 至于HashtableNameValueCollection之间的区别, NameValueCollection可以有重复的键,但Hashtable不能。 Additionally, Hashtable is pretty significantly more efficient in its implementation. 此外, Hashtable在实现方面效率更高。

This article on the MSDN Blog has some good information about Hashtable and NameValueCollection . 关于MSDN博客的这篇文章有一些关于HashtableNameValueCollection好信息。

To summarize their findings, Hashtable is... 总结他们的发现, Hashtable是......

  • 2.6x more efficient on lookup. 查找效率提高2.6倍。
  • 8.5x more efficient on add. 添加效率提高8.5倍。
  • an order of magnitude more efficient on remove. 删除时效率提高了一个数量级。

They wrap up the article with some helpful information on when to use NameValueCollection : 他们总结了一篇关于何时使用NameValueCollection一些有用信息:

So you may be wondering when you'd want to use NameValueCollection. 所以你可能想知道你什么时候想要使用NameValueCollection。 NameValueCollection only accepts keys and values that are Strings, so this is a very specialized collection. NameValueCollection只接受作为字符串的键和值,因此这是一个非常专业的集合。 It's useful in a situation in which you either need to associate multiple values with a key, or to do hash-based lookups as well as lookup by index (and hopefully not perform too many removes). 在需要将多个值与键相关联,或者执行基于散列的查找以及按索引查找(并且希望不执行太多删除)的情况下,它非常有用。

However, if you need to store string key/value pairs and you don't need to perform index-based lookups or associate multiple values with a key, you may prefer to use the generic Dictionary class. 但是,如果需要存储字符串键/值对,并且不需要执行基于索引的查找或将多个值与键关联,则可能更喜欢使用通用的Dictionary类。 This has the same asymptotic behavior as Hashtable in all cases and furthermore avoids any costs due to boxing. 这在所有情况下都具有与Hashtable相同的渐近行为,并且还避免了因拳击造成的任何成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM