C# में Method वो जगह है जहाँ actual काम किया जाता है।
चाहे:
Calculation करना हो
Database में save करना हो
GPS Packet parse करना हो
Bill total निकालना हो
API call करना हो
Game player को move करना हो
सब Method के अंदर लिखा जाता है।
इस blog में methods को आसान तरीके + आपके real GPS project के example से समझेंगे।
Method = Code का छोटा reusable block
हर method का अपना नाम, input, और काम होता है।
void SayHello()
{
Console.WriteLine("Hello!");
}
Call:
SayHello();
returnType MethodName(parameters)
{
// code
}
Example:
int Add(int a, int b)
{
return a + b;
}
Call:
int sum = Add(5, 3);
Code बार-बार लिखने की जरूरत नहीं
Logic clean और organized
Testing आसान
Maintenance simple
Big project छोटा-छोटा बन जाता है
जब G17 / S15 device packet भेजता है,
उसमें कई commands होती हैं:
Location update
Heartbeat
Alarm
SOS
Low battery alert
Status update
हर command को handle करने के लिए अलग method बनता है।
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 का अपना काम:
void HandleGPS(string packet)
{
Console.WriteLine("GPS location updated");
}
void HandleHeartBeat(string packet)
{
Console.WriteLine("Device is online");
}
void HandleAlarm(string packet)
{
Console.WriteLine("Alarm received");
}
इसी तरह बड़े GPS servers में 40–50 methods होते हैं,
हर एक का अपना role।
void SendSMS(string number, string message)
{
Console.WriteLine($"SMS to {number}: {message}");
}
Call:
SendSMS("8888888888", "Your OTP is 1234");
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);
GPS servers, banking, billing software—
हर जगह async methods use होते हैं।
async Task SaveLocationAsync(string imei, double lat, double lon)
{
await database.SaveAsync(imei, lat, lon);
}
double CalculateTotal(double price, int qty)
{
return price * qty;
}
Attendance mark करने के लिए method:
void MarkAttendance(int studentId)
{
Console.WriteLine("Attendance marked for " + studentId);
}
Methods आपके code का दिमाग होते हैं।
इनसे आप:
Logic अलग रखते हो
Bugs कम होते हैं
Code clean रहता है
Project scalable बनता है
Real life में हर काम method करता है।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav