0
0
C Sharp (C#)programming~30 mins

Dictionary key-value collection in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Dictionary key-value collection
📖 Scenario: You are managing a small store's inventory. You want to keep track of product names and their quantities using a dictionary.
🎯 Goal: Create a dictionary to store product names as keys and their quantities as values. Then, update and display the inventory.
📋 What You'll Learn
Create a dictionary called inventory with exact product-quantity pairs
Add a new product quantity variable called newProductQuantity
Use a foreach loop with variables product and quantity to iterate over inventory
Print the product names and their quantities exactly as specified
💡 Why This Matters
🌍 Real World
Stores and businesses use dictionaries to keep track of products and their quantities efficiently.
💼 Career
Understanding dictionaries is essential for managing data collections in software development and database management.
Progress0 / 4 steps
1
Create the inventory dictionary
Create a dictionary called inventory with these exact entries: "Apples" with quantity 30, "Bananas" with quantity 15, and "Oranges" with quantity 25.
C Sharp (C#)
Need a hint?

Use new Dictionary<string, int>() and add the exact key-value pairs inside curly braces.

2
Add a new product quantity variable
Add an integer variable called newProductQuantity and set it to 20.
C Sharp (C#)
Need a hint?

Declare an integer variable with the exact name and value.

3
Iterate over the inventory dictionary
Use a foreach loop with variables product and quantity to iterate over inventory. Inside the loop, add newProductQuantity to each quantity and update the dictionary value.
C Sharp (C#)
Need a hint?

Since you cannot change dictionary values while iterating keys directly, first copy keys to a list, then update values inside the loop.

4
Print the updated inventory
Use a foreach loop with variables product and quantity to iterate over inventory. Print each product and its quantity in the format: "Product: {product}, Quantity: {quantity}".
C Sharp (C#)
Need a hint?

Use Console.WriteLine with string interpolation to print each product and quantity.