-3

I have the method

HandleNotification(string message, Dictionary<string, object> additionalData, bool isActive)

and I would take the from additionalData the value.

I have this additional data:

Extracoins:4

I don't understand how I can take the value 4 from additionalData for a specific key Extracoins.

Mike G
  • 4,232
  • 9
  • 40
  • 66
MisterX_Dev
  • 429
  • 2
  • 7
  • 16

1 Answers1

4

You can get a value from a Dictionary like this if your only interested in accessing one specific key.

object value = null;
additionalData.TryGetValue("Extracoins", out value);

That way object will be the value in the Dictionary or it will remain null if the value is not found.

Or you can do:

if (additionalData.ContainsKey("Extracoins"))
{
    object value = additionalData["Extracoins"];
}

Finally if you wanted to iterate over all the values in the Dictionary until you get the correct value you could do:

object value = null;
foreach (KeyValuePair<string, object> pair in additionalData)
{
    if (pair.Key == "Extracoins")
    {
        value = pair.Value;
    }
}
Marc Harry
  • 2,410
  • 19
  • 21