लोड हो रहा है...

कलेक्शन संदर्भ

C# में कलेक्शन संदर्भ (Collections Reference) डेटा के प्रबंधन और संचालन के लिए आवश्यक संरचनाएँ हैं। ये कलेक्शंस मुख्य रूप से हिप (Heap) पर ऑब्जेक्ट्स का संग्रह करते हैं, जिससे डायनेमिक डेटा हैंडलिंग और लचीलेपन की सुविधा मिलती है। आधुनिक एप्लिकेशन, जैसे कि यूज़र लिस्ट, उत्पाद कैटलॉग, ऑर्डर प्रोसेसिंग या रियल-टाइम इवेंट स्ट्रीमिंग, में बड़े या जटिल डेटा सेट्स को कुशलतापूर्वक संभालने के लिए कलेक्शन संदर्भ अनिवार्य हैं। सही कलेक्शन का चुनाव, जैसे List, Dictionary\, Queue, Stack या HashSet, प्रदर्शन और स्थिर आर्किटेक्चर के लिए महत्वपूर्ण है।
C# कलेक्शन संदर्भ भाषा की मुख्य अवधारणाओं जैसे सटीक सिंटैक्स, डेटा संरचनाएँ, कुशल एल्गोरिदम और ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग (OOP) सिद्धांतों पर आधारित हैं। यह दस्तावेज़ आपको यह सिखाएगा कि कैसे कलेक्शन बनाएं, उन्हें संशोधित करें और ऑप्टिमाइज़ करें। इसमें प्रदर्शन सुधार, त्रुटि प्रबंधन, और डिजाइन पैटर्न जैसे रीयूज़ेबल समाधान भी शामिल हैं। पाठक कलेक्शन संदर्भ का उपयोग करके स्केलेबल, मॉड्यूलर और रखरखाव योग्य सिस्टम विकसित करना सीखेंगे, जो वास्तविक परियोजनाओं में समस्याओं का प्रभावी समाधान प्रदान करते हैं।

मूल उदाहरण

text
TEXT Code
using System;
using System.Collections.Generic;

namespace CollectionReferenceExample
{
class Program
{
static void Main(string\[] args)
{
// String की एक List बनाना
List<string> फल = new List<string> { "सेब", "केला", "चेरी" };

// नया तत्व जोड़ना
फल.Add("संतरा");

// तत्व तक पहुंच और संशोधन
फल[1] = "मैंगो";

// Collection पर iteration
Console.WriteLine("फल की सूची:");
foreach (string item in फल)
{
Console.WriteLine(item);
}

// किसी तत्व की जाँच
if (फल.Contains("चेरी"))
{
Console.WriteLine("चेरी सूची में मौजूद है।");
}
}
}

}

इस उदाहरण में List का उपयोग दिखाया गया है, जो C# में सबसे सामान्य कलेक्शन प्रकारों में से एक है। List फल = new List { "सेब", "केला", "चेरी" }; हिप पर डायनेमिक संग्रह बनाता है, जिससे Collection का आकार आसानी से बढ़ाया जा सकता है। फल.Add("संतरा"); से नया तत्व जोड़ा जाता है और फल[1] = "मैंगो"; से किसी विशेष इंडेक्स पर तत्व को बदला जा सकता है। foreach लूप का उपयोग करके सुरक्षित iteration किया गया है, जो इंडेक्सिंग त्रुटियों को रोकता है। फल.Contains("चेरी") तत्व की खोज के लिए उपयोगी है और यह internal optimized search का उपयोग करता है।
व्यावहारिक परियोजनाओं में List और अन्य कलेक्शन जैसे Dictionary\, Queue, Stack और HashSet का उपयोग यूज़र लिस्ट, इवेंट स्ट्रीम्स और प्रोडक्ट कैटलॉग में किया जाता है। यह OOP सिद्धांतों जैसे encapsulation और modularity का पालन करता है और robust, maintainable C# कोड बनाने में मदद करता है।

व्यावहारिक उदाहरण

text
TEXT Code
using System;
using System.Collections.Generic;

namespace AdvancedCollectionReference
{
class व्यक्ति
{
public string नाम { get; set; }
public int उम्र { get; set; }
}

class Program
{
static void Main(string[] args)
{
// Dictionary बनाना जो unique ID के आधार पर व्यक्ति रखे
Dictionary<int, व्यक्ति> लोग = new Dictionary<int, व्यक्ति>();

लोग.Add(1, new व्यक्ति { नाम = "आलिस", उम्र = 30 });
लोग.Add(2, new व्यक्ति { नाम = "बॉब", उम्र = 25 });
लोग.Add(3, new व्यक्ति { नाम = "चार्ली", उम्र = 35 });

// सुरक्षित खोज
if (लोग.TryGetValue(2, out व्यक्ति मिला))
{
Console.WriteLine($"मिला व्यक्ति: {मिला.नाम}, {मिला.उम्र} वर्ष");
}

// Dictionary पर iteration
foreach (var entry in लोग)
{
Console.WriteLine($"ID: {entry.Key}, नाम: {entry.Value.नाम}");
}

// सुरक्षित removal
लोग.Remove(3);
}
}

}

Advanced C# Implementation

text
TEXT Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace EnterpriseCollectionReference
{
class उत्पाद
{
public string नाम { get; set; }
public decimal मूल्य { get; set; }
}

class Program
{
static void Main(string[] args)
{
try
{
// HashSet से duplicate रोकना
HashSet<उत्पाद> उत्पादक = new HashSet<उत्पाद>(new उत्पादComparer())
{
new उत्पाद { नाम = "लैपटॉप", मूल्य = 1200 },
new उत्पाद { नाम = "टैबलेट", मूल्य = 450 },
new उत्पाद { नाम = "लैपटॉप", मूल्य = 1200 } // ignore होगा
};

// LINQ से filter और sort
var महंगेउत्पाद = उत्पादक
.Where(p => p.मूल्य > 500)
.OrderByDescending(p => p.मूल्य)
.ToList();

foreach (var prod in महंगेउत्पाद)
{
Console.WriteLine($"उत्पाद: {prod.नाम}, मूल्य: {prod.मूल्य}");
}
}
catch (Exception ex)
{
Console.WriteLine($"त्रुटि: {ex.Message}");
}
}
}

class उत्पादComparer : IEqualityComparer<उत्पाद>
{
public bool Equals(उत्पाद x, उत्पाद y)
{
return x.नाम == y.नाम && x.मूल्य == y.मूल्य;
}

public int GetHashCode(उत्पाद obj)
{
return obj.नाम.GetHashCode() ^ obj.मूल्य.GetHashCode();
}
}

}

इन उदाहरणों में Dictionary\ के माध्यम से कुंजी आधारित तेज़ खोज, HashSet से duplicate elimination और LINQ का उपयोग करके sorting और filtering दर्शाया गया है। उत्पादComparer object comparison को customize करता है। Best Practices में exception handling, generic type usage, LINQ का उद्देश्यपूर्ण उपयोग शामिल हैं। सही कलेक्शन का चुनाव (List क्रमबद्ध डेटा के लिए, Dictionary\ key-based lookup के लिए, HashSet unique elements के लिए) प्रदर्शन और memory उपयोग के लिए महत्वपूर्ण है। परियोजनाओं में यह modular systems, repository pattern या event management के लिए उपयोगी है। इन कलेक्शन की समझ जटिल समस्याओं का कुशल समाधान और robust, scalable architecture सुनिश्चित करती है।

C# Best Practices और pitfalls

text
TEXT Code
// Best Practices और सामान्य त्रुटियाँ

* Generic collections का उपयोग करें टाइप सेफ्टी के लिए
* उचित डेटा संरचना चुनें: List<T>, Dictionary\<TKey,TValue>, Queue<T>, Stack<T>, HashSet<T>
* Collection manipulations में exceptions handle करें
* foreach loop का उपयोग iteration के लिए
* Nested loops कम करें performance के लिए
* LINQ का बड़े collections में सावधानीपूर्वक उपयोग करें
* Unused references हटाएँ, Garbage Collector को allow करें
* Large collections में performance test करें
* Multi-threaded access में concurrent collections का उपयोग करें
* Custom Comparers implement करें data integrity के लिए

📊 संपूर्ण संदर्भ

C# Element/Method Description Syntax Example Notes
List<T> Dynamic ordered collection List<int> numbers = new List<int>(); numbers.Add(10); Indexed access, auto resize
List<T>.Add() Element add numbers.Add(10); numbers.Add(5); O(1) average
List<T>.Remove() Element remove numbers.Remove(10); numbers.Remove(5); Returns true if successful
List<T>.Contains() Check element numbers.Contains(10); bool exists = numbers.Contains(5); Linear search
List<T>.Count Number of elements numbers.Count int total = numbers.Count; Dynamic
Dictionary\<TKey,TValue> Key-value collection Dictionary\<int,string> dict = new Dictionary\<int,string>(); dict.Add(1,"Alice"); Fast lookup
Dictionary\<TKey,TValue>.Add() Add key-value dict.Add(1,"Alice"); dict.Add(2,"Bob"); Exception if key exists
Dictionary\<TKey,TValue>.TryGetValue() Safe lookup dict.TryGetValue(2,out string val); bool found = dict.TryGetValue(1,out string name); Avoids exceptions
Dictionary\<TKey,TValue>.Remove() Remove pair dict.Remove(1); dict.Remove(2); True if removed
Dictionary\<TKey,TValue>.Keys Get keys var keys = dict.Keys; foreach(var key in dict.Keys) Read-only
Dictionary\<TKey,TValue>.Values Get values var values = dict.Values; foreach(var val in dict.Values) Read-only
Queue<T> FIFO collection Queue<int> q = new Queue<int>(); q.Enqueue(10); First-In-First-Out
Queue<T>.Enqueue() Add element q.Enqueue(10); q.Enqueue(20) O(1)
Queue<T>.Dequeue() Remove first q.Dequeue(); int first = q.Dequeue(); Exception if empty
Queue<T>.Peek() Peek first q.Peek(); int peek = q.Peek(); Exception if empty
Stack<T> LIFO collection Stack<int> s = new Stack<int>(); s.Push(10); Last-In-First-Out
Stack<T>.Push() Push element s.Push(10); s.Push(20) O(1)
Stack<T>.Pop() Pop element s.Pop(); int top = s.Pop(); O(1), exception if empty
Stack<T>.Peek() Peek top s.Peek(); int top = s.Peek(); Exception if empty
HashSet<T> Unique elements HashSet<int> hs = new HashSet<int>(); hs.Add(10); Avoid duplicates
HashSet<T>.Add() Add element hs.Add(10); hs.Add(5); False if exists
HashSet<T>.Remove() Remove element hs.Remove(10); hs.Remove(5); True if removed
HashSet<T>.Contains() Check element hs.Contains(10); bool exists = hs.Contains(5); O(1) avg
IEnumerable<T> Iteration interface IEnumerable<int> numbers foreach(var n in numbers) Base for foreach and LINQ
ICollection<T> Collection interface ICollection<int> coll coll.Count Supports Count, Add, Remove
IList<T> List interface IList<int> list list\[0] = 10 Indexed access
IReadOnlyCollection<T> Read-only collection IReadOnlyCollection<int> readOnly int total = readOnly.Count No modification
IReadOnlyList<T> Read-only indexed IReadOnlyList<int> readOnlyList int first = readOnlyList\[0] Read-only
ObservableCollection<T> Notifying collection ObservableCollection<int> oc = new ObservableCollection<int>(); oc.CollectionChanged += Handler For MVVM
ConcurrentDictionary\<TKey,TValue> Thread-safe dictionary ConcurrentDictionary\<int,string> cd = new ConcurrentDictionary\<int,string>(); cd.TryAdd(1,"A"); Thread-safe
ConcurrentQueue<T> Thread-safe queue ConcurrentQueue<int> cq = new ConcurrentQueue<int>(); cq.TryDequeue(out int val); FIFO thread-safe
ConcurrentStack<T> Thread-safe stack ConcurrentStack<int> cs = new ConcurrentStack<int>(); cs.TryPop(out int val); LIFO thread-safe
SortedList\<TKey,TValue> Sorted dictionary SortedList\<int,string> sl = new SortedList\<int,string>(); sl.Add(1,"A"); Auto sorted
SortedDictionary\<TKey,TValue> Sorted dictionary SortedDictionary\<int,string> sd = new SortedDictionary\<int,string>(); sd.Add(1,"A"); Better for frequent insertions
LinkedList<T> Doubly linked list LinkedList<int> ll = new LinkedList<int>(); ll.AddLast(10); Sequential access
LinkedList<T>.AddFirst() Add at start ll.AddFirst(5); ll.AddFirst(1) O(1)
LinkedList<T>.AddLast() Add at end ll.AddLast(10); ll.AddLast(20) O(1)
LinkedList<T>.Remove() Remove element ll.Remove(10); ll.Remove(20) O(n)
LinkedList<T>.Find() Find element ll.Find(10); var node = ll.Find(10) Returns LinkedListNode<T>
Stack<T>.Clear() Clear stack s.Clear(); s.Clear() Frees references
Queue<T>.Clear() Clear queue q.Clear(); q.Clear() Frees references
List<T>.Clear() Clear list numbers.Clear(); numbers.Clear() Frees references
Dictionary\<TKey,TValue>.Clear() Clear dictionary dict.Clear(); dict.Clear() Frees references
List<T>.IndexOf() Get index numbers.IndexOf(5); int idx = numbers.IndexOf(5)
ArrayList Non-generic collection ArrayList arr = new ArrayList(); arr.Add(10); Legacy, avoid in modern C#
Queue<T>.Contains() Check element q.Contains(10); bool exists = q.Contains(5) O(n)
Stack<T>.Contains() Check element s.Contains(10); bool exists = s.Contains(5) O(n)
HashSet<T>.UnionWith() Union hs1.UnionWith(hs2); hs1.UnionWith(hs2) Mutating
HashSet<T>.IntersectWith() Intersection hs1.IntersectWith(hs2); hs1.IntersectWith(hs2) Mutating
HashSet<T>.ExceptWith() Difference hs1.ExceptWith(hs2); hs1.ExceptWith(hs2) Mutating
HashSet<T>.SymmetricExceptWith() Symmetric difference hs1.SymmetricExceptWith(hs2); hs1.SymmetricExceptWith(hs2) Mutating
Dictionary\<TKey,TValue>.ContainsKey() Check key dict.ContainsKey(1); bool exists = dict.ContainsKey(2) O(1)
Dictionary\<TKey,TValue>.ContainsValue() Check value dict.ContainsValue("Alice"); bool exists = dict.ContainsValue("Bob") O(n)
Queue<T>.TryPeek() Safe peek q.TryPeek(out int val); bool ok = q.TryPeek(out val) Avoids exceptions
Stack<T>.TryPeek() Safe peek s.TryPeek(out int val); bool ok = s.TryPeek(out val) Avoids exceptions
Stack<T>.TryPop() Safe pop s.TryPop(out int val); bool ok = s.TryPop(out val) Avoids exceptions
Queue<T>.TryDequeue() Safe dequeue q.TryDequeue(out int val); bool ok = q.TryDequeue(out val) Avoids exceptions
List<T>.Insert() Insert at index numbers.Insert(1, 99); numbers.Insert(0,10) Shifts elements
List<T>.RemoveAt() Remove at index numbers.RemoveAt(1); numbers.RemoveAt(0) Shifts elements
Dictionary\<TKey,TValue>.Count Number of pairs dict.Count int total = dict.Count Read-only
HashSet<T>.Count Number of unique elements hs.Count int total = hs.Count Read-only
Queue<T>.Count Number of elements q.Count int total = q.Count Read-only
Stack<T>.Count Number of elements s.Count int total = s.Count Read-only
SortedList\<TKey,TValue>.Count Number of elements sl.Count int total = sl.Count Read-only
LinkedList<T>.Count Number of elements ll.Count int total = ll.Count Read-only
ObservableCollection<T>.Count Number of elements oc.Count int total = oc.Count Read-only
ConcurrentDictionary\<TKey,TValue>.Count Number of elements cd.Count int total = cd.Count Read-only
ConcurrentQueue<T>.Count Number of elements cq.Count int total = cq.Count Read-only
ConcurrentStack<T>.Count Number of elements cs.Count int total = cs.Count Read-only

📊 Complete C# Properties Reference

Property Values Default Description C# Support
Count int 0 Collection में elements की संख्या All Collections
Capacity int 4 Elements before resize List<T>
IsReadOnly bool false Read-only collection? ICollection<T>, IList<T>
Keys TKey Collection N/A Dictionary की keys Dictionary\<TKey,TValue>
Values TValue Collection N/A Dictionary की values Dictionary\<TKey,TValue>
Comparer IEqualityComparer Default Comparer for HashSet/Dictionary HashSet<T>, Dictionary\<TKey,TValue>
SyncRoot object null Synchronization object ICollection
IsSynchronized bool false Thread-safety flag ICollection
CountChanged Event N/A Event when count changes ObservableCollection<T>
CapacityChanged Event N/A Event when capacity changes List<T>
Comparer IComparer Default For Sorted collections SortedList\<TKey,TValue>, SortedDictionary\<TKey,TValue>

C# में कलेक्शन संदर्भ सीखने के मुख्य निष्कर्ष हैं: List, Dictionary, Queue, Stack और HashSet के उचित चयन और उपयोग से memory और performance में सुधार होता है। LINQ और generics का कुशल उपयोग operations को सुरक्षित और तेज़ बनाता है। भविष्य में ObservableCollection, Concurrent Collections और Sorted Collections जैसी उन्नत संरचनाओं का अध्ययन करने से multithreaded और enterprise-level एप्लिकेशन का विकास संभव होता है। इन अवधारणाओं को समझकर, आप scalable, maintainable और robust C# सॉफ़्टवेयर सिस्टम डिज़ाइन कर सकते हैं। परियोजनाओं में सीधे लागू करने के लिए Best Practices और proper error handling का पालन करें। सीखते समय आधिकारिक Microsoft डॉक्स, GitHub प्रोजेक्ट्स और open-source C# libraries उपयोगी संसाधन हैं।

🧠 अपने ज्ञान की परीक्षा करें

शुरू करने के लिए तैयार

Test Your Knowledge

Test your understanding of this topic with practical questions.

4
प्रश्न
🎯
70%
पास करने के लिए
♾️
समय
🔄
प्रयास

📝 निर्देश

  • हर प्रश्न को ध्यान से पढ़ें
  • हर प्रश्न के लिए सबसे अच्छा उत्तर चुनें
  • आप जितनी बार चाहें क्विज़ दोबारा दे सकते हैं
  • आपकी प्रगति शीर्ष पर दिखाई जाएगी