📘 C# Blog Series – Blog 18 **“C# Delegates — Method को Variable की तरह कैसे पास करते हैं?

📘 C# Blog Series – Blog 18

**“C# Delegates — Method को Variable की तरह कैसे पास करते हैं?






(Real-Life Example: Dynamic GPS Packet Parser Delegate)”**

C# में Delegates सबसे शक्तिशाली features में से एक हैं।
Delegate आपको यह क्षमता देते हैं कि:

“एक method को भी variable की तरह pass करो, store करो, और change करो।”

अगर OOP को समझना आसान है,
तो delegates actual power लाते हैं—
और आपके GPS Server में delegates बहुत काम आते हैं:

  • Dynamic packet parser

  • Command handler

  • Notification callbacks

  • Device-specific logic switch

  • Background task callbacks

आज delegates को real-life + GPS use-case के साथ पूरी clarity में समझेंगे।


🟦 1. Delegate होता क्या है? (एक लाइन में)

Delegate =
Method का pointer (यानी reference)
जिससे आप method को variable की तरह इस्तेमाल कर सकते हो।

Real-life example:

  • YouTube पर आप channel link share करते हो

  • Link open → वही channel open होता है
    Delegate = method का link


🟢 2. Basic Delegate Example

Step 1: Delegate define करो

public delegate void MyHandler(string msg);

Step 2: Method बनाओ

void PrintMessage(string msg) { Console.WriteLine(msg); }

Step 3: Delegate में method assign

MyHandler h = PrintMessage; h("Hello C#!");

Delegate ने method को call कर दिया।


🌟 3. Delegate क्यों जरूरी है?

✔ Method को argument की तरह pass कर सकते हो
✔ Different logics choose dynamically
✔ Events delegates पर ही based हैं
✔ High-level flexibility
✔ Strategy pattern implement करना आसान

GPS server में यह बहुत काम आता है।


4. REAL-LIFE GPS EXAMPLE: Dynamic Packet Parser Using Delegate

आप जानते हो कि हर GPS device (G17, S15, A20…) का packet format अलग होता है।

आप चाहो तो हर device के लिए dynamic parser assign कर सकते हो।


🚀 5. Create a Delegate for Packet Parsing

public delegate void PacketParser(string packet);

अब यह delegate किसी भी parsing method को hold कर सकता है।


🚀 6. Parsing Methods (Device Specific)

G17:

void ParseG17(string packet) { Console.WriteLine("Parsed G17 packet"); }

S15:

void ParseS15(string packet) { Console.WriteLine("Parsed S15 packet"); }

🚀 7. Assign Parser Delegate Dynamically

PacketParser parser; string deviceType = "G17"; // assume from IMEI if (deviceType == "G17") parser = ParseG17; else parser = ParseS15; parser(packet); // run dynamic parser

एक ही line में dynamic logic switch
यही “delegate magic” है।


8. Real-Life Example: Server Command Handler Using Delegate

Delegate:

public delegate void CommandHandler(string imei, string cmd);

Different Commands:

void HandleReboot(string imei, string cmd) { } void HandleGetLocation(string imei, string cmd) { } void HandleStopEngine(string imei, string cmd) { }

Assign dynamically:

CommandHandler handler; switch (cmd) { case "REBOOT": handler = HandleReboot; break; case "LOC": handler = HandleGetLocation; break; case "STOP": handler = HandleStopEngine; break; }

अब:

handler(imei, cmd);

Same handler variable → Different behaviors.


9. Real Example: WebSocket Notification Callback

User connected → WebSocket send
User disconnected → alert
Location updated → push live map signal

All these can be delegates.

public delegate void NotifyHandler(GPSDevice d); public NotifyHandler OnLocationUpdate;

Whenever location updates:

OnLocationUpdate?.Invoke(device);

🎮 10. Real-Life Example: Game AI Using Delegates

  • Attack mode

  • Defend mode

  • Patrol mode

All assigned via delegates.


🧠 11. Action, Func, Predicate — Modern Delegates

Old delegate syntax use नहीं करना चाहते?
C# ने 3 ready-made delegates दिए हैं:

✔ Action → No return

Action<string> log = msg => Console.WriteLine(msg);

✔ Func → With return

Func<int, int, int> add = (a, b) => a + b;

✔ Predicate → Returns bool

Predicate<int> isEven = n => n % 2 == 0;

इनसे आपका delegates code ultra clean होता है।


🔚 Conclusion

Delegates C# के सबसे flexible और powerful concepts में से एक हैं।
ये enable करते हैं:

✔ Dynamic logic
✔ Method switch
✔ Event system
✔ Packet parsing
✔ Command routing
✔ Notification callbacks

GPS server architecture में delegates backbone जैसा काम करते हैं—
जिससे आप multiple models, commands और events को extremely clean तरीके से handle कर पाते हो।

Post a Comment

0 Comments

Translate

Close Menu