C# · Chapter 3.10 · Pro level

Constructors, Properties & Encapsulation (Protect Your Player Data)

Constructors, Properties और Encapsulation (प्लेयर का डेटा सुरक्षित रखो)

Until now anyone could write hero.health = -500; and break the game. Professionals protect data — this is called encapsulation. Think of an ATM: you cannot open the cash box (it is private), you can only press buttons that follow rules (public). In C#, keep the field private and give a public property with get (read the value) and set (store a value, with rules). Inside set, the new value is called value, so we can fix bad numbers — e.g. keep health between 0 and 100. A constructor is a special method with the same name as the class and no return type; it runs automatically on new Player("Arjun", 100) and sets the starting values. Finally, overriding ToString() lets Console.WriteLine(hero) print a friendly line instead of just the class name.

अब तक कोई भी hero.health = -500; लिखकर गेम बिगाड़ सकता था। प्रोफ़ेशनल लोग डेटा को सुरक्षित रखते हैं — इसे encapsulation कहते हैं। ATM सोचो: आप पैसों का डिब्बा नहीं खोल सकते (वह private है), आप सिर्फ़ नियम वाले बटन दबा सकते हो (public)। C# में field को private रखो और एक public property दो जिसमें get (वैल्यू पढ़ना) और set (नियम के साथ वैल्यू रखना) हो। set के अंदर नई वैल्यू का नाम value होता है, इसलिए गलत नंबर ठीक कर सकते हैं — जैसे health को 0 से 100 के बीच रखना। Constructor एक ख़ास method है जिसका नाम class जैसा होता है और कोई return type नहीं होता; यह new Player("Arjun", 100) पर अपने-आप चलता है और शुरुआती वैल्यू सेट करता है। आख़िर में, ToString() को override करने से Console.WriteLine(hero) सिर्फ़ class का नाम नहीं, बल्कि एक साफ़ लाइन छापता है।

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

private int health;
A locked field: only code inside the class can touch it.
बंद (locked) field: इसे सिर्फ़ class के अंदर का कोड छू सकता है।
public int Health { get { ... } set { ... } }
A property: get gives the value, set stores it with rules.
Property: get वैल्यू देता है, set नियम के साथ वैल्यू रखता है।
value
Inside set, value is the new number someone is trying to store.
set के अंदर value वह नया नंबर है जिसे कोई रखना चाहता है।
public Player(string name, int startHealth)
Constructor: same name as the class, no return type, runs on new.
Constructor: class जैसा नाम, कोई return type नहीं, new पर चलता है।
public string Name { get; private set; }
Auto-property: everyone can read it, only the class can change it.
Auto-property: सब पढ़ सकते हैं, बदल सिर्फ़ class सकती है।
public override string ToString()
Decides what Console.WriteLine(hero) prints.
तय करता है कि Console.WriteLine(hero) क्या छापेगा।

Ready example

using System;

class Player {
    private int health;
    public string Name { get; private set; }

    public int Health {
        get { return health; }
        set {
            if (value < 0) value = 0;
            if (value > 100) value = 100;
            health = value;
        }
    }

    public Player(string name, int startHealth) {
        Name = name;
        Health = startHealth;
    }

    public void TakeDamage(int damage) {
        Health = Health - damage;
    }

    public override string ToString() {
        return Name + " (HP: " + Health + ")";
    }
}

class Program {
    static void Main() {
        Player hero = new Player("Arjun", 100);
        Console.WriteLine(hero);
        hero.TakeDamage(30);
        Console.WriteLine(hero);
        hero.Health = 500;
        Console.WriteLine("After cheat: " + hero.Health);
        hero.TakeDamage(250);
        Console.WriteLine(hero);
    }
}

What you will see: The console shows HP changing safely: 100 → 70 → (cheat blocked) 100 → 0.

Console में HP सुरक्षित तरीके से बदलती दिखेगी: 100 → 70 → (cheat रुका) 100 → 0।

Line by line

private int health;
Nobody outside the class can write hero.health = -500;.
Class के बाहर से कोई hero.health = -500; नहीं लिख सकता।
set { if (value < 0) value = 0; if (value > 100) value = 100; health = value; }
Every change is checked, so health always stays between 0 and 100.
हर बदलाव जाँचा जाता है, इसलिए health हमेशा 0 से 100 के बीच रहती है।
Player hero = new Player("Arjun", 100);
The constructor sets Name and Health.
Constructor Name और Health सेट करता है।
hero.Health = 500;
The cheat is blocked: Health becomes 100, not 500.
Cheat रुक गया: Health 500 नहीं, 100 बनती है।

Common mistake

✗ Wrong

class Player {
    public void Player(string name) { Name = name; }
}

✓ Right

class Player {
    public Player(string name) { Name = name; }
}

A constructor has NO return type (not even void) and its name must match the class name exactly.

Constructor का कोई return type नहीं होता (void भी नहीं) और उसका नाम बिल्कुल class के नाम जैसा होना चाहिए।

Your task

Complete class Weapon: keep damage between 1 and 50 inside set, add the constructor public Weapon(string name, int dmg), override ToString() to return Name + ": " + Damage + " dmg", then create new Weapon("Sword", 999) and print it — it must show Sword: 50 dmg.

class Weapon पूरी करो: set के अंदर damage को 1 से 50 के बीच रखो, constructor public Weapon(string name, int dmg) जोड़ो, ToString() को override करके Name + ": " + Damage + " dmg" लौटाओ, फिर new Weapon("Sword", 999) बनाकर print करो — Sword: 50 dmg दिखना चाहिए।

  1. In set, before damage = value;, write if (value > 50) value = 50; and if (value < 1) value = 1;.
    set में damage = value; से पहले if (value > 50) value = 50; और if (value < 1) value = 1; लिखो।
  2. Add public Weapon(string name, int dmg) { Name = name; Damage = dmg; } and public override string ToString() { return Name + ": " + Damage + " dmg"; }.
    public Weapon(string name, int dmg) { Name = name; Damage = dmg; } और public override string ToString() { return Name + ": " + Damage + " dmg"; } जोड़ो।
  3. In Main: Weapon sword = new Weapon("Sword", 999); and Console.WriteLine(sword); — then Run.
    Main में: Weapon sword = new Weapon("Sword", 999); और Console.WriteLine(sword); — फिर Run करो।
Starter code for the task
using System;

class Weapon {
    private int damage;
    public string Name { get; private set; }

    public int Damage {
        get { return damage; }
        set {
            // TODO 1: agar value 50 se zyada ho to 50 kar do, aur 1 se kam ho to 1
            damage = value;
        }
    }

    // TODO 2: constructor banao: public Weapon(string name, int dmg) { Name = name; Damage = dmg; }

    // TODO 3: public override string ToString() banao jo  Name + ": " + Damage + " dmg"  return kare
}

class Program {
    static void Main() {
        // TODO 4: Weapon sword = new Weapon("Sword", 999); banao aur Console.WriteLine(sword); karo
    }
}

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

Why do we make a field private and expose a public property?

हम field को private और property को public क्यों बनाते हैं?

  1. To make the program slower
  2. So outside code can change the value only through rules we control (encapsulation)
  3. Because C# does not allow public fields
  4. So the value prints automatically
Show the answer

Answer: B (So outside code can change the value only through rules we control (encapsulation))

The property is the only door to the data, and its set can block bad values like -500 health.

Property ही डेटा तक जाने का अकेला दरवाज़ा है, और उसका set -500 health जैसी गलत वैल्यू रोक सकता है।

Practice problems for this chapter

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