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 का नाम नहीं, बल्कि एक साफ़ लाइन छापता है।
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:
getgives the value,setstores it with rules. - Property:
getवैल्यू देता है,setनियम के साथ वैल्यू रखता है। value- Inside
set,valueis 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 दिखना चाहिए।
- In
set, beforedamage = value;, writeif (value > 50) value = 50;andif (value < 1) value = 1;.setमेंdamage = value;से पहलेif (value > 50) value = 50;औरif (value < 1) value = 1;लिखो। - Add
public Weapon(string name, int dmg) { Name = name; Damage = dmg; }andpublic 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"; }जोड़ो। - In
Main:Weapon sword = new Weapon("Sword", 999);andConsole.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
- A constructor (
public Player(...)) runs onnewand sets starting values — it has no return type. - Encapsulation: keep data
privateand change it only through apublicproperty whosesethas rules. - Override
ToString()soConsole.WriteLine(obj)prints something readable.
- Constructor (
public Player(...))newपर चलता है और शुरुआती वैल्यू सेट करता है — इसका कोई return type नहीं होता। - Encapsulation: data
privateरखो और उसे सिर्फ़ ऐसीpublicproperty से बदलो जिसकेsetमें नियम हों। ToString()override करो ताकिConsole.WriteLine(obj)पढ़ने लायक लाइन छापे।
Quick quiz
Why do we make a field private and expose a public property?
हम field को private और property को public क्यों बनाते हैं?
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
- Fix the Bank Account (encapsulation bugs)
बैंक अकाउंट ठीक करो (encapsulation की गलतियाँ)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.