Search code examples
c#arrayslistinventory

Array used for an inventory like 'Apples' = 99 in C#


I have researched different ways of creating arrays and how to use lists, but I couldn't find what I was looking for. I made a program in python that does what I want, but python cuts alot of corners and I would like to know the "proper" and most efficient way of doing this.

I want to create an array (or list, not sure what's best for this) that has a string like 'Potion' and an int for the amount of that item, like an inventory for a game. What would be the best way to implement this?

I noticed you can make arrays like: Inventory.InvArray[][] But how can I make the first element a string, and the second element an int?

As you can see I'm a little confused, any help is appreciated :)


Solution

  • You are not looking for an array or list, but for a dictionary.

    In .NET, you can use the generic Dictionary<TKey, TValue> Class, e.g.

    var inventory = new Dictionary<string, int>();
    inventory["Apple"] = 99;
    

    In Python, you would use a dict, e.g.

    inventory = dict()
    inventory["Apple"] = 99