Difference between Lookup() and Dictionary(Of list())

前端 未结 6 1385
暖寄归人
暖寄归人 2020-11-27 10:56

I\'m trying to wrap my head around which data structures are the most efficient and when / where to use which ones.

Now, it could be that I simply just don\'t unders

6条回答
  •  感情败类
    2020-11-27 11:19

    Two significant differences:

    • 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.)

    They're likely to be equivalent in efficiency - the lookup may well use a Dictionary> 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>, mostly due to the first two points above.

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

提交回复
热议问题