且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找()和字典之间的差异(名单())

更新时间:2023-01-14 21:06:06

二显著差异:


  • 查找是不可改变的。耶:)(至少,我认为具体的查找类是不可变的,而 ILookup 接口没有提供任何变异的成员,有可以的是其他可变实现的,当然。)

  • 当你查找一个关键这是不是在查找present,你会得到一个空的顺序背,而不是 KeyNotFoundException 。 (因此没有 TryGetValue ,AFAICR。)

  • Lookup is immutable. Yay :) (At least, I believe the concrete Lookup class is immutable, and the ILookup interface doesn't provide any mutating members. There could be other mutable implementations, of course.)
  • When you lookup a key which isn't present in a lookup, you get an empty sequence back instead of a KeyNotFoundException. (Hence there's no TryGetValue, AFAICR.)

他们可能是在效率等同 - 查找可能使用词典< TKEY的,GroupingImplementation< TValue>> 的场景,例如落后。根据您的要求二者之间做出选择。我个人而言,查找通常是一个比一个更适合词典< TKEY的,列表与LT; TValue>> ,主要是由于上面的第一个两分。

They're likely to be equivalent in efficiency - the lookup may well use a Dictionary<TKey, GroupingImplementation<TValue>> behind the scenes, for example. Choose between them based on your requirements. Personally I find that the lookup is usually a better fit than a Dictionary<TKey, List<TValue>>, mostly due to the first two points above.

请注意,作为一个实现细节,具体落实 IGrouping&LT;,&GT; 这是用于数值工具的IList&LT; TValue&GT; ,这意味着它的效率与 COUNT()的ElementAt()等。

Note that as an implementation detail, the concrete implementation of IGrouping<,> which is used for the values implements IList<TValue>, which means that it's efficient to use with Count(), ElementAt() etc.