Why Don't Both Key & Value Need To Be Saved

Obviously, the foreach loop saving only the key works, but I don’t understand why.

I don’t actually understand dictionaries all that well in C#, and I was hoping for a better (any!) explanation as to why both the key and value fields don’t need to be assigned. Since it’s not covered in the course, is there a YouTube tutorial somewhere someone could recommend that covers the topic in more depth? Or is it something that will be covered in the Dialogue course?

Note: same goes for abstract and struct classes.

    object ISaveable.CaptureState()
    {
        var equippedItemsForSerialization = new Dictionary<EquipLocation, string>();
        foreach (var pair in equippedItems)
        {
            equippedItemsForSerialization[pair.Key] = pair.Value.GetItemID();
        }
        return equippedItemsForSerialization;
    }

Actually, we are saving the key and the value.
We build a Dictionary of EquipLocation, string. Each assignment

equippedItemsForSerialization[pair.Key] = pair.Value.GetItemID();

stores the item’s ID in the Dictionary by the Key of the EquipLocation.

Sorry, your response doesn’t actually resolve my confusion - it does point out, though, that my understanding is worse than I thought it was. Since it’s our first exposure to dictionaries, can you recommend another resource that gives a better explanation about how dictionaries, structs, and abstracts work in C#?

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms