From the course: C# Hands-on Practice with Data-Structures

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Implement a dictionary for inventory storage

Implement a dictionary for inventory storage - C# Tutorial

From the course: C# Hands-on Practice with Data-Structures

Implement a dictionary for inventory storage

Let's use a dictionary to manage inventory storage for an application. In this app, we're representing each item as an inventory item, each with its own productCode, name, description, and quantity. This will be used in the InventoryService class. This class will manage our inventory and expose methods to add items, retrieve items, and update their quantities. We can use the dictionary data structure to store our inventory item objects. The key to our dictionary will be the productCode, which is a string, and the value will be our InventoryItem object. This will let us quickly retrieve products by productCode as our inventory grows larger. To get all the items in the inventory, we only need the values of our dictionary, so we'll return inventory.values. Then to get items by productCode, we'll use the TryGetValue method. This will try to get the item with the given productCode as its key. And if it finds it, it will store the value in that item. If the item does not exist, the…

Contents