how to get string and int out of dictionary

so i coded this up but not entirely sure its correct but the goal is to grab the string out of the dictionary along with the int and if they match the input then return true if not return false

public Dictionary<string,int> upgradesDictionary = new Dictionary<string,int>(){
        {"Scoreboard",0},
    };
public bool HasUpgrade(string upgradeName,int upgradeLevel){
        foreach(string upgrade in upgradesDictionary){
            foreach (int level in upgradesDictionary) {
                if (upgradeName == upgrade) {
                    if (upgradeLevel == level) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
        return false;
    }

That shouldn’t even compile…I’m guessing what you want to do is get the int associated with the string value of upgradeName and see if it matches upgradeLevel?

int val;
if (upgradesDictionary.TryGetValue(upgradeName, out val))
{
    return val == upgradeLevel;
}
return false;

shortly after posting this i figured out that wasnt gonna work and actually found this

public bool HasUpgrade(string upgradeName,int upgradeLevel){
        foreach(KeyValuePair<string,int> upgrades in upgradesDictionary){
            if (upgrades.Key == upgradeName) {
                if (upgrades.Value == upgradeLevel) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

it works perfect

My version is shorter and faster :slight_smile: You’re not using a dictionary for what it’s good at - looking stuff up.

im very new to dictionarys lol ive never had a use for one untill this morning i was thinking how on earth am i gonna store 200 upgrades easily