C# · Chapter 3.7 · Intermediate level
Bonus: Hero Inventory Backpack (Arrays [] & foreach)
बोनस: हीरो का हथियार बैग (string[] Array और foreach)
In games like Minecraft, BGMI, or Valorant, a player carries multiple items in one backpack! In C#, a fixed-type list is called an Array (string[] bag = { "Katana", "Shield", "Medkit" };). Its item count is bag.Length, the first item is bag[0], and foreach (string item in bag) inspects every item one by one!
Minecraft या BGMI जैसे हर गेम में हीरो के पास एक Inventory Backpack होता है जिसमें कई हथियार रखे जाते हैं! C# में एक ही टाइप की कई चीज़ें रखने के लिए Array (string[]) बनाते हैं: string[] bag = { "Katana", "Shield", "Medkit" };। पहला आइटम bag[0] होता है और foreach लूप बैग के हर आइटम को एक-एक कर दिखाता है!
Words and symbols in this chapter
string[] bag- Declares an Array of strings — a multi-slot backpack holding text items.
- String Array — कई टेक्स्ट आइटम्स रखने वाला हीरो का बैग।
{ "Sword", "Shield" }- Initializes the array slots at index
[0],[1], etc. - ऐरे के स्लॉट्स (
[0],[1]...) में शुरुआती सामान भरता है। bag[0]- Reads the very first item in the array (0-based indexing!).
- बैग का सबसे पहला आइटम निकालता है (गिनती
0से शुरू होती है!)। bag.Length- Gives the total count of items inside
bag. - बताता है कि
bagमें कुल कितने आइटम हैं। foreach (string item in bag)- Loops cleanly over every element inside
bagwithout manual counters. bagके अंदर मौजूद हरitemपर अपने-आप एक-एक बार चलता है।
Ready example
using System;
class Program {
static void Main() {
string[] bag = { "Dragon Katana", "Golden Shield", "Health Potion" };
Console.WriteLine("Total Items: " + bag.Length);
Console.WriteLine("Equipped [0]: " + bag[0]);
foreach (string item in bag) {
Console.WriteLine("Slot -> " + item);
}
}
}
What you will see: Look at the 2D Game Stage HUD — a glowing 🎒 Inventory Array bar appears showing [0], [1], [2]!
दाईं तरफ़ 2D गेम स्टेज में ऊपर देखें — हीरो का 🎒 Inventory Array बैग हर स्लॉट ([0], [1], [2]) के साथ चमक रहा है!
Line by line
string[] bag = { "Dragon Katana", ... };- Creates a 3-slot weapon inventory array and lights up the HUD backpack!
- 3 आइटम्स वाला हथियार बैग बनाता है जो गेम स्क्रीन के HUD में भी दिखेगा!
bag.Length / bag[0]bag.Lengthis3, andbag[0]is"Dragon Katana".bag.Lengthकुल संख्या3देता है औरbag[0]पहला हथियार देता है।foreach (string item in bag) { ... }- Prints every item in the hero's inventory cleanly.
- बैग के तीनों आइटम्स को एक-एक करके कंसोल पर प्रिंट करता है।
Common mistake
✗ Wrong
string[] bag = [ "Sword", "Shield" ];✓ Right
string[] bag = { "Sword", "Shield" };C# array values are wrapped in curly braces { ... }, whereas Python lists use [ ... ]!
याद रखो: Python लिस्ट में [ ] लगता है, लेकिन C# ऐरे की वैल्यूज़ { ... } कर्ली ब्रैकेट में लिखी जाती हैं!
Your task
Create a string[] array with at least 3 game items and loop through them using foreach!
string[] ऐरे में कम से कम 3 गेम आइटम रखो और foreach लूप से सबको प्रिंट करके ▶ Run & Preview दबाओ!
- Declare
string[] bag = { "...", "...", "..." };with 3 items.string[] bag = { ... };में अपने मनपसंद 3 हथियार/आइटम लिखो। - Use
foreach (string item in bag)andConsole.WriteLine(item);.foreach (string item in bag)लगाकरConsole.WriteLine(item);करो। - Click ▶ Run & Preview to see your 🎒 Inventory HUD!
▶ Run & Preview दबाकर गेम स्क्रीन पर अपना बैग देखो!
Starter code for the task
using System;
class Program {
static void Main() {
// TODO 1: string[] bag = { "Laser Sword", "Armor", "Magic Orb" }; banao
// TODO 2: Console.WriteLine("Bag Size: " + bag.Length); likho
// TODO 3: foreach (string item in bag) { Console.WriteLine(item); } se har item print karo
}
}Do this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
- An array holds many values of one type:
string[] bag = { "Sword", "Shield" };. bag[0]is the first item andbag.Lengthis the number of items.foreach (string item in bag)visits every item without index maths.
- Array एक ही टाइप की कई वैल्यू रखता है:
string[] bag = { "Sword", "Shield" };। bag[0]पहला item है औरbag.Lengthकुल items की संख्या।foreach (string item in bag)बिना index गिने हर item पर चलता है।
Quick quiz
How do you get the number of items inside a C# array bag?
C# ऐरे bag में कुल कितने आइटम हैं, यह कैसे पता चलता है?
Show the answer
Answer: C (bag.Length)
In C#, arrays expose the .Length property (with capital L)!
C# में ऐरे की कुल लंबाई bag.Length से मिलती है!
Practice problems for this chapter
- Predict: The Hero's Backpack (array + foreach)
बताओ: हीरो का बैग (array + foreach)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.