I want to Count the items in an associated array. I have written small piece of code but now I am thinking it will not work. What strategy should I use?
I have created a class
public class itemsList
{
public string itemName;
public int itemCount;
}
Calculation code is as follows (consider items in the following code an array)
foreach (var item in items)
{
//this is pseudo, I want help in this
if (itemsList.Contain("itemName", item)
itemsList[item].itemCount++;
else
itemsList.Add(item, 1);
}
Please keep in mind this array has later to be changed to json in the following format.
"apples": 10,
"oranges": 4
....
itemsList[item]comes back with nothing. Then the key doesn't exist