Why can't Dictionaries have the same variables

When the game first all the highscore points are set to 0 and “empty”. I’m using a dictionary to keep the score and name together so how come when I put it into a for statement it doesn’t like the variables being the same? And since it happens would it be best to use a dictionary especially if two scores end up being exactly the same?

 for(int i = 0; i < 6; i++)
            {
                highscoreList.Add(0, "empty");
            }

HI,
as the C# documation said , you can’t add duplicate keys in dictionaries.all keys should be unique.
link text

if you want duplicate keys you can use list’s of keyvaluepairs. like this:

List<KeyValuePair<string, string>> myduplicateLovingDictionary= new List<KeyValuePair<string, string>>();
KeyValuePair<string,string> myItem = new KeyValuePair<string,string>(dialedno, line);
myduplicateLovingDictionary.Add(myItem);

@LemonCore56 There are some ways to update add/update/read values in a dictionary.
This might be the simplest form that you can retry in my opinion.


And to answer your question “why you cannot call Add(0,“empty”) multiple times”, it is not allowed is because dictionary data structure does not allow you to add key-value pairs with duplicated keys. To demonstrate, if you have a contact list in your phone and you created 2 contacts with different numbers but you named both of them as “Adam”, the next time when you ask your friend to find Adam’s number, your lucky friend will of course get confused whether which Adam you are looking for.


However, in your use case of dictionary, I suppose you can update it using the following code.


Dictionary<string, int> dict = new Dictionary<string, int>();

//initialize key-value pairs
dict["score"] = 0;  //key "score", initial value 0
dict["lives"] = 3;  //key "lives", initial value 3

//update
dict["score"] = 100;
dict["lives"] -= 1;

I wish this helps!