.NET dictionary with two keys and one value

前端 未结 13 2963
盖世英雄少女心
盖世英雄少女心 2020-12-14 00:23

Is there a dictionary available in .NET that could hold 2 keys and one value. Like

Dictionary(Of TKey, Of TKey, TValue)

I have

13条回答
  •  执念已碎
    2020-12-14 00:42

    As you wish your value to be “findable” from either key, I would just use two dictionaries like you are doing now. However I would wrap this up in a class, with methods names like FindByXXX and FindByYYY.

    The much harder question is how do you do a delete, as you need to know both keys at the time of the delete. Maybe your value stores both keys so you can pass the value into your delete method. Maybe you never need to remove items from the dictionaries. Or the code that needs to remove items knows both keys.

    Hence there is no standard dictionary to do this, as the requirements are different between each user.

    (Note you don’t want a dictionary with a composite key, as that would require you to know both keys whenever you wished to look up an item.)

提交回复
热议问题