Build a video game inventory system that stores, searches, and sorts items like a pro! (Ages 8-18)
CSharp-Lesson-Seven/
├── .git/
├── .gitignore
└── collections/
├── collections.csproj
├── GameItem.cs ← you will create this in Step 4
└── Program.cs
Starting at Step 4, this lesson uses two C# files: GameItem.cs holds the class, and Program.cs holds the Main method that uses it. Each step header tells you which file(s) to change — and from Step 5 onward, only Program.cs changes while GameItem.cs stays exactly the same.
When a step says "replace Program.cs with this" (or GameItem.cs), click inside that file, select all (Ctrl+A / Cmd+A), delete it, then paste the new code. Keeping the class in its own file means you will never accidentally end up with two Main methods or leftover starter code mixed in with your class.
You only need to do this once, in Step 4, to make GameItem.cs:
- In the Solution Explorer on the left, right-click the
collectionsproject (the one with the little C# icon, not the outer folder). - Choose Add → Class…
- Type
GameItemfor the name and press Enter. - Rider creates
GameItem.cswith an empty class. Select all (Ctrl+A / Cmd+A), delete it, then paste the Step 4 snippet below.
- What to do: Go to https://github.com/PhillyCoderDojo/CSharp-Lesson-Seven → Click "Fork" → Open GitHub Desktop → Clone your fork
- File(s) touched: Creating project folder
- Code snippet: None
- Why it matters: Get your workspace for managing collections
- Git command:
git clone https://github.com/YOUR-USERNAME/CSharp-Lesson-Seven.git
- What to do: Open Rider → File → Open → Select your cloned folder → Click OK. The project lives in the
collections/subfolder. - File(s) touched: Loading project
- Code snippet: None
- Why it matters: Ready to master Lists and collections!
- Git command: None
- What to do: Replace
Program.cswith the code below. It builds a simple shopping list usingList<string>and prints it. (No new files yet — that comes in Step 4.) - File(s) touched: Program.cs
- Code snippet for
Program.cs(full file):
namespace collections;
class Program
{
static void Main(string[] args)
{
List<string> shoppingList = new List<string>();
shoppingList.Add("Apples");
shoppingList.Add("Bread");
shoppingList.Add("Milk");
Console.WriteLine("Shopping List:");
foreach (string item in shoppingList)
{
Console.WriteLine($"• {item}");
}
Console.WriteLine($"Total items: {shoppingList.Count}");
}
}- Why it matters: Lists store multiple items that can grow/shrink
- Git command:
git add . && git commit -m "Create first List with basic operations"
- What to do: First, create a brand-new file called
GameItem.csusing the "How to create a new C# file in Rider" steps above. Paste theGameItem.cssnippet below into it. Then replaceProgram.cswith the snippet that creates oneGameItemand prints it to confirm everything compiles. - File(s) touched: GameItem.cs (new), Program.cs
- Code snippet for
GameItem.cs(full file):
namespace collections;
class GameItem
{
public string Name { get; set; }
public int Value { get; set; }
public string Type { get; set; }
public GameItem(string name, int value, string type)
{
Name = name;
Value = value;
Type = type;
}
public override string ToString()
{
return $"{Name} ({Type}) - ${Value}";
}
}- Code snippet for
Program.cs(full file):
namespace collections;
class Program
{
static void Main(string[] args)
{
GameItem sword = new GameItem("Magic Sword", 150, "Weapon");
Console.WriteLine(sword);
}
}- Why it matters: Lists can hold custom objects, not just strings — and one class per file is how real C# projects are organized.
- Git command:
git add . && git commit -m "Create GameItem class in its own file"
- What to do: Replace
Program.cswith the version below.Mainnow creates aList<GameItem>inventory, prints every item, adds a new one, then removes the Health Potion.GameItem.csdoes not change in this step. - File(s) touched: Program.cs
- Code snippet for
Program.cs(full file):
namespace collections;
class Program
{
static void Main(string[] args)
{
List<GameItem> inventory = new List<GameItem>
{
new GameItem("Magic Sword", 150, "Weapon"),
new GameItem("Health Potion", 25, "Consumable"),
new GameItem("Steel Shield", 75, "Armor"),
new GameItem("Fire Spell", 100, "Magic")
};
Console.WriteLine("=== Player Inventory ===");
foreach (GameItem item in inventory)
{
Console.WriteLine(item);
}
inventory.Add(new GameItem("Dragon Scale", 200, "Material"));
Console.WriteLine($"\nFound Dragon Scale! Inventory now has {inventory.Count} items.");
inventory.RemoveAt(1); // Remove Health Potion
Console.WriteLine("Used Health Potion!");
}
}- Why it matters: Real programs manage collections of objects
- Git command:
git add . && git commit -m "Create game inventory with object list"
- What to do: Replace
Program.cswith the version below. After the inventory is built,Mainuses LINQ (Where,OrderByDescending) to find expensive items, list all weapons, and sort by value.GameItem.csstill does not change. - File(s) touched: Program.cs
- Code snippet for
Program.cs(full file):
using System.Linq;
namespace collections;
class Program
{
static void Main(string[] args)
{
List<GameItem> inventory = new List<GameItem>
{
new GameItem("Magic Sword", 150, "Weapon"),
new GameItem("Health Potion", 25, "Consumable"),
new GameItem("Steel Shield", 75, "Armor"),
new GameItem("Fire Spell", 100, "Magic")
};
Console.WriteLine("=== Player Inventory ===");
foreach (GameItem item in inventory)
{
Console.WriteLine(item);
}
inventory.Add(new GameItem("Dragon Scale", 200, "Material"));
Console.WriteLine($"\nFound Dragon Scale! Inventory now has {inventory.Count} items.");
inventory.RemoveAt(1); // Remove Health Potion
Console.WriteLine("Used Health Potion!");
Console.WriteLine("\n=== Expensive Items (Value > $100) ===");
var expensiveItems = inventory.Where(item => item.Value > 100);
foreach (var item in expensiveItems)
{
Console.WriteLine(item);
}
Console.WriteLine("\n=== All Weapons ===");
var weapons = inventory.Where(item => item.Type == "Weapon");
foreach (var weapon in weapons)
{
Console.WriteLine(weapon);
}
Console.WriteLine("\n=== Items Sorted by Value (High to Low) ===");
var sortedItems = inventory.OrderByDescending(item => item.Value);
foreach (var item in sortedItems)
{
Console.WriteLine(item);
}
}
}- Why it matters: LINQ makes searching and sorting super easy
- Git command:
git add . && git commit -m "Add LINQ search and sorting features"
- What to do: Replace
Program.cswith the version below. It puts everything together: awhileloop with a menu, three helper methods, and LINQ for the searches.GameItem.csstill does not change. - File(s) touched: Program.cs
- Code snippet for
Program.cs(full file):
using System.Linq;
namespace collections;
class Program
{
static void Main(string[] args)
{
List<GameItem> inventory = new List<GameItem>
{
new GameItem("Magic Sword", 150, "Weapon"),
new GameItem("Health Potion", 25, "Consumable"),
new GameItem("Steel Shield", 75, "Armor"),
new GameItem("Fire Spell", 100, "Magic"),
new GameItem("Iron Dagger", 50, "Weapon")
};
while (true)
{
Console.WriteLine("\n=== Inventory Manager ===");
Console.WriteLine("1. Show All Items 2. Search by Type 3. Expensive Items 4. Quit");
Console.Write("Choose option: ");
string choice = Console.ReadLine();
if (choice == "1")
{
ShowAllItems(inventory);
}
else if (choice == "2")
{
SearchByType(inventory);
}
else if (choice == "3")
{
ShowExpensiveItems(inventory);
}
else if (choice == "4")
{
break;
}
}
}
static void ShowAllItems(List<GameItem> items)
{
Console.WriteLine("\n=== All Items ===");
foreach (var item in items.OrderBy(i => i.Name))
{
Console.WriteLine(item);
}
Console.WriteLine($"Total value: ${items.Sum(i => i.Value)}");
}
static void SearchByType(List<GameItem> items)
{
Console.Write("Enter item type (Weapon/Armor/Magic/Consumable): ");
string type = Console.ReadLine();
var filtered = items.Where(i => i.Type.Equals(type, StringComparison.OrdinalIgnoreCase));
Console.WriteLine($"\n=== {type} Items ===");
foreach (var item in filtered)
{
Console.WriteLine(item);
}
}
static void ShowExpensiveItems(List<GameItem> items)
{
var expensive = items.Where(i => i.Value >= 100).OrderByDescending(i => i.Value);
Console.WriteLine("\n=== Expensive Items ($100+) ===");
foreach (var item in expensive)
{
Console.WriteLine(item);
}
}
}- Why it matters: Combine lists, LINQ, and methods for complete programs
- Git command:
git add . && git commit -m "Create interactive inventory manager"
- What to do: In GitHub Desktop → Commit changes → Push origin
- File(s) touched: Uploading to GitHub
- Code snippet: None
- Why it matters: Share your powerful collection management system
- Git command:
git push origin main
- Press Ctrl+F5 or click green
▶️ button in Rider - Expected output after Step 7: a menu loop. Press
1to list all items alphabetically with the total value,2then type a type likeWeaponto filter,3for items worth $100+,4to quit.
- "The name 'GameItem' does not exist in the current context" —
Program.cscannot see your class. Check thatGameItem.csis in the same project folder asProgram.cs(you should see it listed in the Solution Explorer under thecollectionsproject) and that both files start withnamespace collections;. - "Type 'Program' already defines a member called 'Main'" — You pasted the
Program.cssnippet on top of itself instead of replacing the file's contents. Select all (Ctrl+A / Cmd+A) insideProgram.cs, delete everything, then paste once. There should be exactly onestatic void Main(string[] args)inProgram.csand zero inGameItem.cs. - "The entry point of the program is global code; ignoring 'Main(string[])' entry point" — A stray line of code is sitting outside any class — often a leftover
Console.WriteLine("Hello, World!");from the starterProgram.cs. Make sure every line inProgram.csis inside eitherclass Program { ... }orstatic void Main(string[] args) { ... }. - "The name 'Where' (or 'OrderBy', 'OrderByDescending', 'Sum') does not exist" — Missing
using System.Linq;at the top ofProgram.cs. Add it as the very first line. - "GameItem does not contain a constructor that takes 0 arguments" — You wrote
new GameItem()instead ofnew GameItem(name, value, type). TheGameItemconstructor needs all three values.
Create a PlayerStats tracker with a List of achievements — and put it in its own new file called PlayerStats.cs (use the same "Add → Class…" trick from Step 4).
Hint (paste into PlayerStats.cs):
namespace collections;
class PlayerStats
{
public string PlayerName { get; set; }
public List<string> Achievements { get; set; }
public Dictionary<string, int> Stats { get; set; }
public PlayerStats(string name)
{
PlayerName = name;
Achievements = new List<string>();
Stats = new Dictionary<string, int>
{
{"Level", 1},
{"Experience", 0},
{"Gold", 100}
};
}
public void AddAchievement(string achievement)
{
if (!Achievements.Contains(achievement))
{
Achievements.Add(achievement);
Console.WriteLine($"🏆 Achievement unlocked: {achievement}");
}
}
}