Reputation: 89
I have a function for stock many string associate to int:
public int Scale(string value)
{
this.stringToInt = new Dictionary<string, int>()
{
{"1p",00},{"2p",01},{"3p",03} ... {"300p",40}
};
// Here i try to do something like that: if(value == (String in dictionary) return associate int
}
So i try to do a comparaison between string receive in enter and string in my dictionary for return associate int.
Any idea?
Thanks you for help !
Upvotes: 3
Views: 61
Reputation: 12171
You can use ContainsKey()
method of Dictionary
to check if the key is present in dictionary:
if (this.stringToInt.ContainsKey(value)
{
return this.stringToInt[value];
}
else
{
// return something else
}
Another way is to use TryGetValue()
:
var valueGot = this.stringToInt.TryGetValue(value, out var associate);
if (valueGot)
{
return associate;
}
else
{
// return something else
}
Upvotes: 6