new to c#.
I’m trying to make a simple system where I can search through a list of objects and when it belongs to a certain ID. Put it in a List/ArrayList so I can read it out/do anything else.
Each “item” has a locationID. When I enter this location and press “look”. The program should check which location I’m in and take out only the “items” with the same ID.
In practise. The list of items is going to be quite big (and locations too). So it seems a waste of processing to go through the list again and again. That’s why I thought of using a dictionary. But I don’t seem to be able to access multiple entries in the dictionary, only at one specific place.
for example:
Dictionary<string, int> itemLoc = new Dictionary<string, int>();
itemLoc.Add("pen", 011);
itemLoc.Add("paper", 011);
itemLoc.Add("tv remote", 012);
//print everything in location 011
Sorry for a lack of code. I’m still figuring out how to get my head around this. I’ve been researching ArrayLists, Lists, Hashtables, and these Dictionaries. But with none I’ve been able to achieve what I’m looking for effectively.
I could do a foreach, and check each entry. But like mentioned above. That would seem a waste of processing.
Any idea’s or tips?
A nudge in the right direction code-wise would be really appreciated.
You haven’t given us enough information as to the constraints of your problem. A
Dictionary<string, List<string>>will perform faster, but you haven’t identified if you can have multiple items in multiple locations (for example, can “paper” be in 011 and 012 or just in 011?) In that case, every time you add you will have to make sure that item is not already in another location.A very simplified approach would be to break it down very simplistically, and optimize based on your results.
Once you perform your benchmark, you can consider switching it to something (perhaps a
Dictionary<string, List<string>>