📘 C# Blog Series – Blog 6 “C# Properties — Get/Set क्या होते हैं और Real-Life में कैसे काम करते हैं?”

📘 C# Blog Series – Blog 6

“C# Properties — Get/Set क्या होते हैं और Real-Life में कैसे काम करते हैं?”





C# में Properties सबसे ज्यादा इस्तेमाल होने वाली चीज़ है।
Real-life में जब भी data को:

  • read करना हो

  • update करना हो

  • validate करना हो

  • secure रखना हो

तब get/set properties का उपयोग किया जाता है।

आज इसे बहुत आसान उदाहरण + आपके GPS Server वाले real use-case से समझेंगे।


🟦 1. Property होती क्या है?

Property एक सरल तरीका है data को सुरक्षित तरीके से get/set करने का
ये variable की तरह दिखती है, लेकिन अंदर logic लगा सकते हैं।

Basic Example:

public class Student { public string Name { get; set; } }

Use:

Student s = new Student(); s.Name = "Rahul"; // set Console.WriteLine(s.Name); // get

🟢 2. Property क्यों चाहिए? (Why Not Public Variables?)

अगर fields public कर दो, तो कोई भी गलत value set कर सकता है:

student.Marks = -50; // गलत

But properties allow:

  • Validation

  • Control

  • Logic

  • Read-only / Write-only

इसीलिए properties OOP में बहुत जरूरी हैं।


🌟 3. Real-Life Example: GPS Device Location Update

आपका GPS Server जब भी packet receive करता है,
latitude & longitude update करता है।

लेकिन —
कुछ गलत data आ सकता है:

  • Latitude 200 degree 😅

  • Longitude -500 degree

  • Or null value

ये allowed नहीं है।

Properties से validation आसानी से हो जाता है।

C# Model:

class GPSDevice { private double latitude; private double longitude; public double Latitude { get { return latitude; } set { if (value >= -90 && value <= 90) latitude = value; } } public double Longitude { get { return longitude; } set { if (value >= -180 && value <= 180) longitude = value; } } }

अब device गलत data भेजे तो भी आपका server safe रहेगा।


4. Auto Properties (Most Common in Projects)

जब कोई extra logic नहीं चाहिए:

public string IMEI { get; set; } public int Battery { get; set; }

Backend APIs, Entity Framework, DTOs में 90% auto properties use होती हैं।


5. Read-Only Property

अगर user सिर्फ data पढ़ सके → पर बदल न सके:

public class User { public string Username { get; } public User(string name) { Username = name; } }

Real-life example:
Login पर username fixed रहता है।


6. Write-Only Property

अगर value सिर्फ set हो सके (rare use-case):

private string password; public string Password { set { password = value; } }

Real-life example:
Password write होता है, read नहीं दिखाया जाता।


7. Property with Logic (Important)

Billing software में quantity negative नहीं हो सकती।

private int qty; public int Quantity { get { return qty; } set { if (value >= 0) qty = value; } }

🔥 Real-Life Property Use Cases (आपके प्रोजेक्ट में भी)**

✔ GPS Server

  • Battery Percentage validate

  • Speed validate

  • Track Status update

  • Packet type clean

✔ Billing/Invoicing

  • Quantity validation

  • Price validation

  • Total amount calculated property

✔ Web API Models

  • DTOs

  • Database Entities

  • Input validation

✔ Game Development

  • Player health

  • Player score

  • Fuel level

Properties हर जगह छाए हुए हैं।


🔚 Conclusion

Properties data को सुरक्षित, clean, और controlled तरीके से रखती हैं।
C# के modern codebase में properties backbone की तरह होती हैं:

  • Get/Set

  • Validation

  • Read-only logic

  • Auto properties

  • Value protection

ये सीख कर आप clean और professional-level code लिखते हो।

Post a Comment

0 Comments

Translate

Close Menu