BCA / B.Tech 13 min read

Generic Collections in .NET (C#) in Hindi

Generic Collections in .NET (C#) | .NET मे Generic Collections हिंदी में


Generic Collections क्या होती हैं?

  • Generic Collections ऐसी डेटा संरचनाएँ होती हैं जो type-safe होती हैं और किसी भी प्रकार (data type) के तत्वों को संग्रहीत (store) कर सकती हैं, 
  • बिना यह चिंता किए कि उन्हें किस प्रकार के डेटा (like int, string, etc.) के रूप में उपयोग किया जाएगा। 
  • इसका मुख्य फायदा यह है कि आप compile-time पर ही यह सुनिश्चित कर सकते हैं कि गलत प्रकार का डेटा संग्रहण में न आए।
  • उदाहरण के लिए, यदि हम एक list बनाते हैं जो केवल integer (int) मानों को संग्रहित करेगी, तो Generic Collections हमें यह सुनिश्चित करने में मदद करती हैं 
  • कि उस list में केवल int ही डालें और किसी और प्रकार का डेटा जैसे string, float आदि उसमें न जाए।
  • Generics .NET (C#) में powerful और flexible tools हैं, जो हमें type-safe, reusable और performance-friendly collections बनाने में मदद करते हैं। 
  • List<T> और Dictionary<TKey, TValue> जैसी collections generics का उपयोग करती हैं, जो आपको विभिन्न प्रकार के डेटा के साथ काम करने की सुविधा देती हैं।
Generics हमारे लिए क्या कर सकते हैं?

Generics के इस्तेमाल से हम कुछ महत्वपूर्ण फायदे प्राप्त कर सकते हैं:

  • Type Safety: Generics के साथ, हम compile-time पर type checking कर सकते हैं, जिसका मतलब है कि आपको run-time errors कम होंगे। उदाहरण के तौर पर, अगर आप एक List<int> बना रहे हैं, तो उसमें int के अलावा कोई और type नहीं जा सकता।
  • Code Reusability: Generics हमें एक ही डेटा संरचना (data structure) को कई प्रकार के डेटा के साथ इस्तेमाल करने की सुविधा देते हैं। इससे हमारा कोड ज्यादा reusable और maintainable बनता है।
  • Performance Improvement: Generics हमें boxing और unboxing जैसी समस्याओं से बचाते हैं, जो non-generic collections में आती हैं। इससे performance बेहतर होती है क्योंकि डेटा को अपनी मूल type में ही संग्रहित किया जाता है।
  • Generic List<T> Collection : List<T> एक generic collection है, जो किसी भी प्रकार के डेटा को सूची (list) के रूप में संग्रहीत करता है। यहां T डेटा प्रकार (data type) का प्रतिनिधित्व करता है।

उदाहरण:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Integer type की List
        List<int> numbers = new List<int>();

        // List में मान (values) जोड़ें
        numbers.Add(10);
        numbers.Add(20);
        numbers.Add(30);

        // List के प्रत्येक तत्व को print करें
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}

इसमें List<int> एक generic list है जो केवल integer (int) प्रकार के डेटा को स्टोर कर सकती है। आप इसे किसी और प्रकार जैसे string, double आदि के लिए भी बना सकते हैं, जैसे:

List<string> names = new List<string>();
names.Add("John");
names.Add("Sara");
Dictionary<TKey, TValue> Collection

Dictionary<TKey, TValue> एक generic collection है जो key-value pairs को स्टोर करता है। इसमें TKey वह type है जो key का प्रतिनिधित्व करता है, और TValue वह type है जो value का प्रतिनिधित्व करता है। Dictionary में हर key की एक unique value होती है, जो उसके साथ जुड़ी होती है।

उदाहरण:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Dictionary, जहां key type string और value type int है
        Dictionary<string, int> ageDictionary = new Dictionary<string, int>();

        // Dictionary में data जोड़ें
        ageDictionary.Add("John", 25);
        ageDictionary.Add("Sara", 30);
        ageDictionary.Add("Mike", 35);

        // Dictionary के सभी key-value pairs को print करें
        foreach (var pair in ageDictionary)
        {
            Console.WriteLine($"Name: {pair.Key}, Age: {pair.Value}");
        }
    }
}

इसमें Dictionary<string, int> एक dictionary है, जहां key string प्रकार का है (व्यक्ति का नाम) और value int प्रकार की है (व्यक्ति की उम्र)। आप इसे किसी और प्रकार के data के लिए भी उपयोग कर सकते हैं जैसे:

Dictionary<int, string> empDict = new Dictionary<int, string>();
empDict.Add(1, "John");
empDict.Add(2, "Sara");

Advantages of Generics in Hindi | Generics के लाभ :

  • Type Safety: जैसा कि पहले बताया, Generics compile-time पर type checking करते हैं, जिससे runtime errors कम होते हैं।
  • Performance: Non-generic collections में boxing और unboxing होता है (जब value types को reference types में बदलना होता है), जो performance को प्रभावित करता है। Generics इन समस्याओं से बचाते हैं।
  • Reusable Code: एक बार generic class या method बना लेने के बाद, आप उसे विभिन्न प्रकार के डेटा के लिए पुनः उपयोग कर सकते हैं, बिना हर बार नया class लिखने की जरूरत के।

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 in Hindi | CLR क्या है?
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