BCA / B.Tech 13 min read

Generic Collections in .NET (C#) in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: .Net Notes in Hindi | डॉट नेट नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Generic Collections in .NET (C#) in Hindi - General Topics
  • • अवलोकन: Quick Answer: .NET और C# में Generic Collections (नेमस्पेस: System.Collections.Generic) ऐसी strongly-typed data structures हैं जो compile-time पर type-s...
  • • मुख्य अवधारणाएं: NET generic collections C#, List Dictionary Queue Stack Hindi, HashSet performance collections,.NET generic collections C#, List Dictionary Queue Stack, HashSet performance collections
Quick Answer: .NET और C# में Generic Collections (नेमस्पेस: System.Collections.Generic) ऐसी strongly-typed data structures हैं जो compile-time पर type-safety सुनिश्चित करती हैं और runtime पर Boxing और Unboxing की आवश्यकता को पूरी तरह समाप्त कर देती हैं। .NET 1.0 में पुराने non-generic collections (जैसे ArrayList और Hashtable) हर element को object मानकर store करते थे, जिससे type casting errors और memory overhead होता था। .NET 2.0 में पेश की गई generic collections जैसे List<T>, Dictionary<TKey, TValue>, Queue<T>, और Stack<T> high performance, type-safe और reusable code लिखने की सुविधा देती हैं।

Generic Collections क्या हैं और इनकी आवश्यकता क्यों पड़ी?

C# प्रोग्रामिंग में जब हमें runtime पर एक साथ कई elements को manage करना होता है, तो हम collections का use करते हैं। C# भाषा की बुनियादी जानकारी के लिए आप introduction of C# in .NET in Hindi पढ़ सकते हैं।

शुरुआती .NET 1.1 वर्जन में केवल Non-Generic Collections (जैसे ArrayList) उपलब्ध थीं। इनकी दो सबसे बड़ी कमियां थीं:

  1. Type Safety का अभाव: ArrayList में किसी भी data type (int, string, double, student object) को एक साथ डाला जा सकता था। यदि डेवलपर गलती से string की जगह int डाल देता था, तो compile-time पर कोई warning नहीं मिलती थी, लेकिन runtime पर InvalidCastException से application crash हो जाती थी।
  2. Boxing और Unboxing का Performance Overhead: जब किसी value type (जैसे int) को ArrayList में डाला जाता था, तो CLR उसे heap memory पर reference type (object) में बदलता था (Boxing)। data वापस निकालते समय फिर से unboxing करनी पड़ती थी। लाखों records के लिए यह process CPU और memory को बहुत धीमा कर देती थी। C# में value types की समझ के लिए structs in C# .NET देखें।

इन दोनों समस्याओं को जड़ से खत्म करने के लिए Microsoft ने Generic Collections बनाईं, जहाँ <T> एक Type Parameter होता है जो compile-time पर ही तय कर देता है कि collection केवल उसी specific type के data को स्वीकार करेगी।

Advertisement

Non-Generic बनाम Generic Collections (Comparison Table)

BCA/MCA थ्योरी परीक्षाओं में non-generic और generic collections के बीच तुलना सबसे महत्वपूर्ण प्रश्न है:

Feature / Parameter Non-Generic Collections Generic Collections
Namespace System.Collections System.Collections.Generic
Type Safety नहीं (सभी items System.Object के रूप में store होते हैं)। हाँ (Compile-time पर type checking होती है)।
Boxing & Unboxing Value types के लिए बार-बार boxing और unboxing अनिवार्य होती है। बिल्कुल नहीं होती; data सीधे अपने native format में store होता है।
Performance धीमी (Memory allocation और type checking overhead). बहुत तेज (Direct memory access और zero casting overhead).
Error Catching Type errors केवल Runtime पर पकड़े जाते हैं। Type mismatch होने पर Compile-Time पर ही error आ जाती है।
आधुनिक उपयोग Legacy कोड के अलावा आज उपयोग नहीं की जाती। Modern C# और .NET में डिफ़ॉल्ट उद्योग मानक (Industry Standard).

C# में मुख्य Generic Collections और उनके समतुल्य (Equivalents)

यह तालिका स्पष्ट करती है कि किस पुराने non-generic collection के स्थान पर कौन सा modern generic collection प्रयोग किया जाता है:

Old Non-Generic Class Modern Generic Class Data Structure & Behavior Ideal Real-World Use Case
ArrayList List<T> Dynamically resizable array, index-based access. Items की ordered list (जैसे products, student names).
Hashtable Dictionary<TKey, TValue> Hash table आधारित key-value pairs (Unique keys). Fast lookup (जैसे Roll Number से Student Details खोजना).
Queue Queue<T> FIFO (First In First Out) बफर. Print jobs, background email sending queue.
Stack Stack<T> LIFO (Last In First Out) मेमोरी. Undo/Redo ऑपरेशन, expression parsing, browser history.
(कोई समकक्ष नहीं) HashSet<T> Unique elements का unordered set. Duplicates हटाना और mathematical set operations (Union, Intersect).

Generic Collections के 4 मुख्य लाभ (Key Advantages)

  1. Compile-Time Type Safety: यदि आप List<int> बनाते हैं और उसमें list.Add("Hello") लिखने का प्रयास करते हैं, तो C# compiler तुरंत एरर दे देगा। इससे runtime पर application crash होने की संभावना समाप्त हो जाती है।
  2. High Execution Performance: Boxing और Unboxing न होने के कारण CPU cycles की बचत होती है और Garbage Collector (GC) पर memory pressure बहुत कम पड़ता है।
  3. Clean & Readable Code: Data निकालते समय (Student)list[0] जैसी भद्दी type casting करने की जरूरत नहीं होती। C# अपने आप variable को सही type में लौटाता है।
  4. Code Reusability: Generic classes और methods को एक बार लिखकर अलग-अलग data types (int, float, custom classes) के साथ बार-बार use किया जा सकता है।

Complete Worked Program: C# Generic Collections का व्यावहारिक उपयोग

नीचे दिए गए C# प्रोग्राम में List<T>, Dictionary<TKey, TValue>, और Queue<T> का पूरा व्यावहारिक उपयोग दिखाया गया है:

using System;
using System.Collections.Generic;

namespace GenericCollectionsDemo {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("=== 1. Generic List<string> Example ===");
            List<string> courses = new List<string>();
            courses.Add("BCA");
            courses.Add("MCA");
            courses.Add("B.Tech");

            Console.WriteLine("Total Courses: " + courses.Count);
            foreach (string course in courses) {
                Console.WriteLine("Course Name: " + course);
            }

            Console.WriteLine("\n=== 2. Generic Dictionary<int, string> Example ===");
            Dictionary<int, string> studentRecords = new Dictionary<int, string>();
            studentRecords.Add(101, "Aman Sharma");
            studentRecords.Add(102, "Pooja Verma");
            studentRecords.Add(103, "Rohan Singh");

            // Fast lookup by Key
            int searchId = 102;
            if (studentRecords.ContainsKey(searchId)) {
                Console.WriteLine("Student Found for Roll No " + searchId + ": " + studentRecords[searchId]);
            }

            Console.WriteLine("\n=== 3. Generic Queue<string> (FIFO) Example ===");
            Queue<string> printJobs = new Queue<string>();
            printJobs.Enqueue("Marksheet.pdf");
            printJobs.Enqueue("Syllabus.docx");
            printJobs.Enqueue("IdCard.png");

            // Processing queue
            while (printJobs.Count > 0) {
                string currentJob = printJobs.Dequeue();
                Console.WriteLine("Printing File: " + currentJob);
            }

            Console.WriteLine("\nExecution finished successfully.");
        }
    }
}
Advertisement

Program Output:

=== 1. Generic List<string> Example ===
Total Courses: 3
Course Name: BCA
Course Name: MCA
Course Name: B.Tech

=== 2. Generic Dictionary<int, string> Example ===
Student Found for Roll No 102: Pooja Verma

=== 3. Generic Queue<string> (FIFO) Example ===
Printing File: Marksheet.pdf
Printing File: Syllabus.docx
Printing File: IdCard.png

Execution finished successfully.

विद्यार्थी अक्सर क्या गलतियां करते हैं? (Common Student Mistakes)

  • नेमस्पेस भूल जाना: Generic collections का उपयोग करने के लिए फाइल के सबसे ऊपर using System.Collections.Generic; लिखना अनिवार्य है। पुराने छात्र अक्सर गलती से using System.Collections; लिख देते हैं जिससे generic classes नहीं मिलतीं।
  • Dictionary में Duplicate Key जोड़ना: Dictionary में सभी keys unique होनी चाहिए। यदि आप dict.Add(101, "Name") दोबारा उसी key 101 के साथ कॉल करते हैं, तो C# ArgumentException फेंक देता है। safe code के लिए हमेशा dict.ContainsKey(key) से जांच करें या indexer (dict[key] = value) का use करें।
  • Boxing को केवल syntax समझना: कई छात्र परीक्षा में boxing/unboxing को केवल variable assignment समझ लेते हैं। उत्तर में यह स्पष्ट करना जरूरी है कि boxing में stack से heap पर memory copy होती है, जिससे performance खराब होती है और Generics इसी overhead को रोकते हैं।

Exam के लिए Important Questions

Short Questions (2 से 5 अंक)

  1. Generic Collections क्या हैं? .NET में इनका मुख्य नेमस्पेस क्या है?
  2. ArrayList और List<T> में 2 मुख्य अंतर बताइए।
  3. Boxing और Unboxing क्या होता है और Generics इसे कैसे रोकते हैं?
  4. Dictionary<TKey, TValue> और Hashtable में कौन अधिक सुरक्षित है और क्यों?
  5. HashSet<T> कलेक्शन की क्या मुख्य खूबी होती है?

Long Questions (10 से 20 अंक)

  1. C# .NET में Generic Collections की संकल्पना को उपयुक्त उदाहरणों के साथ समझाइए। Non-generic collections की तुलना में इनके मुख्य लाभों का विश्लेषण कीजिए।
  2. List<T>, Dictionary<TKey, TValue>, और Queue<T> के उपयोग को दर्शाते हुए एक संपूर्ण C# प्रोग्राम लिखिए और इसके आउटपुट की व्याख्या कीजिए। अधिक परीक्षा प्रश्नों के लिए .NET with C# important questions MDSU देखें।
  3. .NET Framework में Generics के आर्किटेक्चर, Type Parameters, और Constraints (जैसे where T : class) के तकनीकी महत्व का समीक्षात्मक वर्णन कीजिए।

FAQs

Q1: क्या Generic Collection में custom objects को store किया जा सकता है?

हाँ, आप अपनी बनाई गई किसी भी custom class (जैसे List<Student> या Dictionary<int, Employee>) के objects को generic collection में सीधे store कर सकते हैं।

Q2: क्या Generic Collections Thread-Safe होती हैं?

नहीं, System.Collections.Generic की डिफ़ॉल्ट collections thread-safe नहीं होतीं। Multi-threaded environment के लिए .NET System.Collections.Concurrent नेमस्पेस (जैसे ConcurrentBag<T>, ConcurrentDictionary<TKey, TValue>) देता है।

Q3: क्या List<T> का आकार (Capacity) dynamically बढ़ता है?

हाँ, List<T> internally एक array का उपयोग करती है। जब elements की संख्या current capacity से अधिक हो जाती है, तो .NET internally array का size दोगुना (double) कर देता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: .NET और C# में Generic Collections (नेमस्पेस: System.Collections.Generic) ऐसी strongly-typed data structures हैं जो compile-time पर type-safety सुनिश्चित।
.Net Notes in Hindi | डॉट नेट नोट्स हिंदी में के पाठ्यक्रम में Generic Collections in .NET (C#) in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Generic Collections in .NET (C#) in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Generic Collections in .NET (C#) in Hindi
Introduction of .Net in Hindi
Web Services in Hindi | वेब सर्विसेस हिंदी में
WSDL in Hindi | WSDL हिंदी में
Boxing & Unboxing in ADO.NET in Hindi
CLR Full Form – CLR क्या है (Common Language Runtime)
Common Types System in Hindi
MSIL in Hindi
Assemblies & Class Libraries in Hindi
Project of .Net in Hindi
What is VB.NET and IDE in Hindi | वीबी.नेट क्या है ?
Intermediate Language in Hindi
Object Orientation in Hindi
Managed Execution in Hindi
Rapid Development in Hindi
Windows Presentation Foundation in Hindi
Whats new For .NET framework 3.5?
Windows Workflow Foundation (WWF) in Hindi
Windows Card Space in Hindi
Windows Communication Foundation in Hindi
How To Install and Use The Visual Studio 2008
How to Working With Visual Studio 2008
Types of Visual Studio 2008 in Hindi
Visual Studio 2008 IDE in Hindi
How To Create Console Application in Hindi
Introduction of C# in .NET in Hindi
Classes of .NET With C# in Hindi
Properties of .NET With C# in Hindi
Structs in C# .NET in Hindi
Delegates & Events in Hindi
Type Safety in Hindi
Nullable Types in .NET in Hindi
ADO.NET in Hindi | ADO.NET क्या है?
SQL Connection Object in Hindi
SQL Command in Hindi
LINQ in Hindi | LINQ क्या है?
What is Using Stored Procedures?
Windows Application in .NET in Hindi | Windows Application क्या है?
BCA || .NET with C# 2023 Paper | MDSU Exam Paper
.NET with C# All Important Questions and Answers in Hindi (MDSU)
BCA || .NET with C# 2025 Paper | MDSU Exam Paper