How to add duplicate keys into the Dictionary

后端 未结 6 700
闹比i
闹比i 2021-01-31 17:37

I have some lines from text files that i want to add into the Dictionary.I am using Dictionary for the first time.While adding up starting lines it was Ok but suddenly i got err

6条回答
  •  滥情空心
    2021-01-31 18:20

    how to allow to add duplicate keys in Dictionary

    It is not possible. All keys should be unique. As Dictionary implemented:

    Every key in aDictionary must be unique according to the dictionary's equality comparer.

    Possible solutions - you can keep collection of strings as value (i.e. use Dictionary>), or (better) you can use Lookup instead of dictionary.


    how to check for duplicate keys and delete previous value from Dictionary?

    You can check if the key exists with previousLines.ContainsKey(dialedno) but if you always want to hold the last line, then just replace whatever dictionary had for the key, or add the new key if it is not in the dictionary:

    previousLines[dialedno] = line;
    

提交回复
热议问题