Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में Dictionary क्लास


C# में Dictionary कुंजियों और मानों का एक संग्रह है। यह System.Collection.Generics नेमस्पेस में एक जेनेरिक संग्रह क्लास है।

सिंटैक्स

निम्नलिखित सिंटैक्स है -

public class Dictionary<TKey,TValue>

यहां, कुंजी पैरामीटर डिक्शनरी में कुंजियों का प्रकार है, जबकि TValue मानों का प्रकार है।

उदाहरण

आइए अब एक Dictionary बनाते हैं और कुछ तत्व जोड़ते हैं -

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "John"); 
        dict.Add("Two", "Tom"); 
        dict.Add("Three", "Jacob"); 
        dict.Add("Four", "Kevin"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("Key/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
    } 
}

आउटपुट

यह निम्नलिखित आउटपुट देगा -

Key/value pairs... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan

दूसरा उदाहरण

अब, आइए एक उदाहरण देखते हैं और कुछ कुंजियों को हटाते हैं -

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "Kagido"); 
        dict.Add("Two", "Ngidi"); 
        dict.Add("Three", "Devillers"); 
        dict.Add("Four", "Smith"); 
        dict.Add("Five", "Warner"); 
        Console.WriteLine("Count of elements = "+dict.Count); 
        Console.WriteLine("Removing some keys..."); 
        dict.Remove("Four"); 
        dict.Remove("Five"); 
        Console.WriteLine("Count of elements (updated) = "+dict.Count); 
        Console.WriteLine("\nKey/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
        Console.Write("\nAll the keys..\n"); 
        Dictionary<string, string>.KeyCollection allKeys = dict.Keys; 
        foreach(string str in allKeys){ 
            Console.WriteLine("Key = {0}", str); 
        } 
    } 
}

आउटपुट

यह निम्नलिखित आउटपुट देगा -

Count of elements = 5 Removing some keys... Count of elements (updated) = 3 Key/value pairs... Key = One, Value = Kagido Key = Two, Value = Ngidi Key = Three, Value = Devillers All the keys.. Key = One Key = Two Key = Three
  1. - डेटा संरचना में डिक्शनरी ऑपरेशन

    डिक्शनरी को ऑब्जेक्ट्स के समूह को संग्रहीत करने के लिए एक जनरल-पर्पस डेटा संरचना के रूप में परिभाषित किया जाता है। एक डिक्शनरी कुंजियों के एक सेट से जुड़ी होती है और प्रत्येक कुंजी का एक संबद्ध मान होता है। जब एक कुंजी प्रस्तुत की जाती है, तो डिक्शनरी केवल संबद्ध मान लौटाएगी।उदाहरण के लिए, कक्षा परी

  1. - C# में Convert क्लास का उपयोग

    Convert क्लास में एक बेस डेटा टाइप को दूसरे बेस डेटा टाइप में बदलने के मेथड्स होते हैं। कुछ उदाहरण देखते हैं: Convert.ToBoolean() मेथड निर्दिष्ट मान को समतुल्य बूलियन मान में बदलता है। सिंटैक्स public static bool ToBoolean (string val, IFormatProvider provider); यहां, val एक स्ट्रिंग है जिसमें True

  1. C# में Console क्लास C# में Console क्लास

    C# में Console क्लास का उपयोग कंसोल एप्लिकेशन के लिए मानक इनपुट, आउटपुट और त्रुटि स्ट्रीम को दर्शाने के लिए किया जाता है। आइए C# में Console क्लास प्रॉपर्टीज के कुछ उदाहरण देखते हैं: Console.CursorLeft प्रॉपर्टी C# में कंसोल का CursorLeft बदलने के लिए Console.CursorLeft प्रॉपर्टी का उपयोग करें। उदा