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

Dictionary methods and access patterns in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Dictionary methods and access patterns
📖 Scenario: You are managing a small store's inventory. You want to keep track of product names and their quantities using a dictionary.
🎯 Goal: Build a C# program that creates a dictionary of products and quantities, checks for a specific product, updates quantities, and prints the final inventory.
📋 What You'll Learn
Create a dictionary called inventory with exact product names and quantities
Create a variable called productToCheck with the exact value "Bananas"
Use the ContainsKey method to check if productToCheck is in inventory
If the product exists, increase its quantity by 10 using the indexer
Print the final inventory dictionary contents in the format Product: Quantity
💡 Why This Matters
🌍 Real World
Managing store inventory is a common task where dictionaries help track product quantities efficiently.
💼 Career
Understanding dictionary methods and access patterns is essential for software developers working with data collections and real-world applications.
Progress0 / 4 steps
1
Create the inventory dictionary
Create a dictionary called inventory with these exact entries: "Apples" with quantity 50, "Bananas" with quantity 30, and "Oranges" with quantity 20.
C Sharp (C#)
Need a hint?

Use new Dictionary<string, int>() and initialize with curly braces containing key-value pairs.

2
Add a product to check
Create a string variable called productToCheck and set it to the exact value "Bananas".
C Sharp (C#)
Need a hint?

Use string productToCheck = "Bananas"; to create the variable.

3
Check and update the product quantity
Use if (inventory.ContainsKey(productToCheck)) to check if productToCheck is in inventory. If it is, increase its quantity by 10 using inventory[productToCheck] += 10;.
C Sharp (C#)
Need a hint?

Use ContainsKey to check presence, then update with the indexer.

4
Print the final inventory
Use a foreach loop with var item in inventory to print each product and its quantity in the format Product: Quantity using Console.WriteLine($"{item.Key}: {item.Value}");.
C Sharp (C#)
Need a hint?

Use a foreach loop and Console.WriteLine with string interpolation.