📘 C# Blog Series – Blog 7 “C# Methods — Logic कैसे लिखा जाता है? (Real-Life Example: GPS Command Processing)”

📘 C# Blog Series – Blog 7

“C# Methods — Logic कैसे लिखा जाता है? (Real-Life Example: GPS Command Processing)”






C# में Method वो जगह है जहाँ actual काम किया जाता है।
चाहे:

  • Calculation करना हो

  • Database में save करना हो

  • GPS Packet parse करना हो

  • Bill total निकालना हो

  • API call करना हो

  • Game player को move करना हो

सब Method के अंदर लिखा जाता है।

इस blog में methods को आसान तरीके + आपके real GPS project के example से समझेंगे।


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

Method = Code का छोटा reusable block
हर method का अपना नाम, input, और काम होता है।

Example:

void SayHello() { Console.WriteLine("Hello!"); }

Call:

SayHello();

🟢 2. Method के Parts

returnType MethodName(parameters) { // code }

Example:

int Add(int a, int b) { return a + b; }

Call:

int sum = Add(5, 3);

3. Method क्यों ज़रूरी है?

  • Code बार-बार लिखने की जरूरत नहीं

  • Logic clean और organized

  • Testing आसान

  • Maintenance simple

  • Big project छोटा-छोटा बन जाता है


🌟 4. Real-Life Example (सबसे Important!)

GPS Server में Commands कैसे Process होते हैं?

जब G17 / S15 device packet भेजता है,
उसमें कई commands होती हैं:

  • Location update

  • Heartbeat

  • Alarm

  • SOS

  • Low battery alert

  • Status update

हर command को handle करने के लिए अलग method बनता है।

Example:

void ProcessPacket(string packet) { if (packet.Contains("GPS")) HandleGPS(packet); else if (packet.Contains("HB")) HandleHeartBeat(packet); else if (packet.Contains("ALARM")) HandleAlarm(packet); }

अब हर function का अपना काम:

GPS packet:

void HandleGPS(string packet) { Console.WriteLine("GPS location updated"); }

Heartbeat:

void HandleHeartBeat(string packet) { Console.WriteLine("Device is online"); }

Alarm:

void HandleAlarm(string packet) { Console.WriteLine("Alarm received"); }

इसी तरह बड़े GPS servers में 40–50 methods होते हैं,
हर एक का अपना role।


5. Methods with Parameters

void SendSMS(string number, string message) { Console.WriteLine($"SMS to {number}: {message}"); }

Call:

SendSMS("8888888888", "Your OTP is 1234");

6. Methods with Return Type

double GetDistance(double lat1, double lon1, double lat2, double lon2) { // calculate & return return 5.6; }

Use:

double km = GetDistance(26.85, 80.94, 26.90, 81.00);

7. Async Methods (Most Important in Web/API)

GPS servers, banking, billing software—
हर जगह async methods use होते हैं।

async Task SaveLocationAsync(string imei, double lat, double lon) { await database.SaveAsync(imei, lat, lon); }

8. Real-Life Example 2: Billing Software

Method to calculate total:

double CalculateTotal(double price, int qty) { return price * qty; }

9. Real-Life Example 3: School System

Attendance mark करने के लिए method:

void MarkAttendance(int studentId) { Console.WriteLine("Attendance marked for " + studentId); }

🔚 Conclusion

Methods आपके code का दिमाग होते हैं।

इनसे आप:

  • Logic अलग रखते हो

  • Bugs कम होते हैं

  • Code clean रहता है

  • Project scalable बनता है

Real life में हर काम method करता है।

Post a Comment

0 Comments

Translate

Close Menu