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

C# में Dictionary.Item[] प्रॉपर्टी


C# में Dictionary.Item[] प्रॉपर्टी का उपयोग Dictionary में निर्दिष्ट कुंजी से जुड़े मान को प्राप्त या सेट करने के लिए किया जाता है।

सिंटैक्स

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

public TValue this[TKey key] { get; set; }

उदाहरण

आइए अब Dictionary.Item[] प्रॉपर्टी को लागू करने का एक उदाहरण देखें:

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "Chris"); 
        dict.Add("Two", "Steve"); 
        dict.Add("Three", "Messi"); 
        dict.Add("Four", "Ryan"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("Count of elements = " + 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.WriteLine("Value for key three = " + dict["Three"]); 
        dict["Three"] = "Ronaldo"; 
        Console.Write("Updated value associated with key Three..."); 
        Console.WriteLine(dict["Three"]); 
        if (dict.ContainsValue("Angelina")) 
            Console.WriteLine("\n\nValue found!"); 
        else 
            Console.WriteLine("\n\nValue isn't in the dictionary!"); 
        dict.Clear(); 
        Console.WriteLine("Cleared Key/value pairs..."); 
        foreach(KeyValuePair<string, string> res in dict){ 
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); 
        } 
        Console.WriteLine("Count of elements now = " + dict.Count); 
    } 
}

आउटपुट

यह निम्नलिखित आउटपुट उत्पन्न करेगा:

Count of elements = 5 
Key/value pairs... 
Key = One, Value = Chris 
Key = Two, Value = Steve 
Key = Three, Value = Messi 
Key = Four, Value = Ryan 
Key = Five, Value = Nathan 
Value for key three = Messi 
Updated value associated with key Three...Ronaldo 
Value isn't in the dictionary! 
Cleared Key/value pairs... 
Count of elements now = 0

उदाहरण

आइए अब Dictionary.Item[] मेथड को लागू करने का एक और उदाहरण देखें:

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(){ 
        Dictionary<string, string> dict = new Dictionary<string, string>(); 
        dict.Add("One", "Chris"); 
        dict.Add("Two", "Steve"); 
        dict.Add("Three", "Messi"); 
        dict.Add("Four", "Ryan"); 
        dict.Add("Five", "Nathan"); 
        Console.WriteLine("Count of elements = " + 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.WriteLine("Value for key three = " + dict["Three"]); 
        dict["Three"] = "Katie"; 
        Console.Write("Updated value associated with key Three..."); 
        Console.WriteLine(dict["Three"]); 
    } 
}

आउटपुट

यह निम्नलिखित आउटपुट उत्पन्न करेगा:

Count of elements = 5 
Key/value pairs... 
Key = One, Value = Chris 
Key = Two, Value = Steve 
Key = Three, Value = Messi 
Key = Four, Value = Ryan 
Key = Five, Value = Nathan 
Value for key three = Messi 
Updated value associated with key Three...Katie

  1. HTML DOM Meter value प्रॉपर्टी HTML DOM Meter value प्रॉपर्टी

    परिचय HTML DOM Meter value प्रॉपर्टी <meter> एलिमेंट के value एट्रिब्यूट से संबंधित एक नंबर वापस/सेट करती है। बेहतर परिणामों के लिए high, low, min और max एट्रिब्यूट्स के साथ उपयोग करें। नोट: <meter> का उपयोग प्रोग्रेस बार के रूप में न करें बल्कि केवल गेज के रूप में उपयोग करें। सिंटैक्स

  1. HTML DOM nodeValue प्रॉपर्टी HTML DOM nodeValue प्रॉपर्टी

    परिचय HTML DOM nodeValue प्रॉपर्टी नोड के मान से संबंधित एक स्ट्रिंग लौटाती/सेट करती है। सिंटैक्स स्ट्रिंग मान वापस करना: Node.nodeValue यहां, रिटर्न मान निम्न हो सकते हैं: डॉक्यूमेंट नोड्स और एलिमेंट नोड्स के लिए null मान एट्रिब्यूट नोड्स के लिए एट्रिब्यूट का मान टेक्स्ट नोड्स और कमेंट नोड्स के

  1. HTML DOM value प्रॉपर्टी HTML DOM value प्रॉपर्टी

    HTML DOM value प्रॉपर्टी HTML DOM value प्रॉपर्टी एक एलिमेंट के एट्रिब्यूट के मान से संबंधित एक स्ट्रिंग लौटाती है। सिंटैक्स स्ट्रिंग मान प्राप्त करना elementAttribute.value उदाहरण आइए HTML DOM value प्रॉपर्टी का एक उदाहरण देखें: <!DOCTYPE html> <html> <head> <title>HTML