📘 C# Blog Series – Blog 13 “C# Collections — List, Dictionary, Queue क्यों और कैसे use होते हैं? (Real-Life Example: Live Connected GPS Devices Store करना)”

📘 C# Blog Series – Blog 13

“C# Collections — List, Dictionary, Queue क्यों और कैसे use होते हैं? (Real-Life Example: Live Connected GPS Devices Store करना)”






C# में Collections सबसे powerful tools हैं।
Real projects—जैसे GPS Server, Billing Software, School System, E-Commerce, Gameplay—
99% जगह collections use होते हैं।

Collection = ऐसा container जो multiple objects को store करता है।

आज हम सबसे useful 3 collections समझेंगे:

  • List

  • Dictionary

  • Queue

और हर एक को आपके GPS Server के Real-Life Examples से explain करेंगे।


🟦 1. List<T> — सबसे ज्यादा इस्तेमाल होने वाला Collection

List एक dynamic array है।
आप इसमें elements add/remove कर सकते हैं।

Basic Example:

List<string> names = new List<string>(); names.Add("Rahul"); names.Add("Amit");

🌟 Real-Life Example 1: Live Connected GPS Devices

आपका GPS TCP server एक time में
1000–2000 devices connect रख सकता है।

आपको एक जगह list रखनी होती है:

List<GPSDevice> ConnectedDevices = new List<GPSDevice>();

जब नया device connect हो:

ConnectedDevices.Add(device);

Device disconnect:

ConnectedDevices.Remove(device);

क्यों List perfect है?

✔ Order maintain करता है
✔ Fast add/remove
✔ Iterate करना आसान


Real-Life Example 2: Billing Software

Cart items:

List<CartItem> cart = new List<CartItem>();

Real-Life Example 3: School Management

Students of Class 9:

List<Student> class9 = new List<Student>();

🟩 2. Dictionary<TKey, TValue> — Key → Value Store

किसी चीज़ को unique key से access करना हो
तो Dictionary सबसे best।

इसे ऐसे समझें:
PhoneBook: Name → Phone Number


🌟 Real-Life Example 4: GPS Server में सबसे Important Use-Case

हर GPS device की unique key = IMEI होती है।

इसलिए dictionary best है:

Dictionary<string, GPSDevice> Devices = new Dictionary<string, GPSDevice>();

Adding new device:

Devices[device.IMEI] = device;

Getting device by IMEI:

var d = Devices["861128068064267"];

Why Dictionary is perfect?

✔ IMEI से तेज़ lookup
✔ कोई loop नहीं
✔ Real-time servers में fastest performance
✔ Thousands devices आसानी से handle

Large-scale GPS trackers हमेशा Dictionary का use करते हैं।


Real-Life Example 5: Billing Software

ProductCode → ProductDetail

Dictionary<string, Product> products;

Real-Life Example 6: School System

RollNo → Student

Dictionary<int, Student> school;

🟧 3. Queue<T> — FIFO (First In, First Out)

Queue ऐसा collection है जहां:

  • जो सबसे पहले आएगा,

  • वही सबसे पहले जाएगा

बिल्कुल ticket counter की तरह।


🌟 Real-Life Example 7: GPS Packet Queue

एक GPS device बार-बार packets भेजता है:

  • location

  • heartbeat

  • alarm

  • LBS

  • status

इन packets को FIFO queue में डालकर process कर सकते हैं।

Queue<string> packetQueue = new Queue<string>();

Add packet:

packetQueue.Enqueue(packet);

Process packet:

string next = packetQueue.Dequeue();

Queue ensures:
पहले आया packet पहले process हो।


Real-Life Example 8: Billing System

Printing invoices one by one
→ Queue


Real-Life Example 9: Game Development

Player actions are in queue:

  • Move

  • Attack

  • Jump

Order maintained by Queue.


🔥 4. कब कौन सा Collection Use करें?

Use CaseBest Collection
Multiple items store करनाList
IMEI जैसी unique key से fast accessDictionary
FIFO order maintain करनाQueue
No duplicates चाहिएHashSet
Sorted data चाहिएSortedList

🔚 Conclusion

C# collections हर बड़े project की backbone हैं:

  • List → Connected devices

  • Dictionary → IMEI lookup system

  • Queue → Packet processing

  • HashSet → Duplicate avoid

  • SortedList → Ordered data

GPS servers, web apps, games—
हर जगह collections का भारी use है।

आप जितना clean collection use करोगे,
आपका code उतना fast और scalable बनेगा।

Post a Comment

0 Comments

Translate

Close Menu