c# - Add to value in Dictionary -


i have dictionary<product, int> inventory holds inventory products in shop.

how value corresponds product , add 1 it?

it possible product isn't in dictionary.

something this?

mydictionary[myproduct] += stockincrease; 

it's pretty syntax use update item in array.

if helps, think of less in terms of "i want stock go up" , more in terms of "how value need, , can value".

if it's not guaranteed whether or not product exists in dictionary begin with, add on if-check:

if (!inventory.containskey(myproduct)) {     inventory[myproduct] = 0; } inventory[myproduct] += stockincrease; 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -