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 लूप बैग के हर आइटम को एक-एक कर दिखाता है!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

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 bag without 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.Length is 3, and bag[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 दबाओ!

  1. Declare string[] bag = { "...", "...", "..." }; with 3 items.
    string[] bag = { ... }; में अपने मनपसंद 3 हथियार/आइटम लिखो।
  2. Use foreach (string item in bag) and Console.WriteLine(item);.
    foreach (string item in bag) लगाकर Console.WriteLine(item); करो।
  3. 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

Quick quiz

How do you get the number of items inside a C# array bag?

C# ऐरे bag में कुल कितने आइटम हैं, यह कैसे पता चलता है?

  1. len(bag)
  2. bag.Size()
  3. bag.Length
  4. bag.CountAll
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

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.