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

C# में SortedDictionary.Item[] प्रॉपर्टी - कुंजी द्वारा मान प्राप्त/सेट करें

परिचय

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

सिंटैक्स

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

key प्राप्त या सेट किए जाने वाले मान की कुंजी है।

उदाहरण 1: बुनियादी उपयोग और Clear

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(1, "One");
        sortedDict.Add(2, "Two");
        sortedDict.Add(3, "Three");
        sortedDict.Add(4, "Four");
        sortedDict.Add(5, "Five");
        sortedDict.Add(6, "Six");

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);

        sortedDict.Add(7, "Seven");
        sortedDict.Add(8, "Eight");

        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);

        Console.WriteLine("कुंजी पांच का मान = " + sortedDict[5]);

        sortedDict.Clear();
        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
    }
}

आउटपुट

Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
SortedDictionary कुंजी-मान जोड़ों की संख्या = 6

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
Key = 7, Value = Seven
Key = 8, Value = Eight
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 8
कुंजी पांच का मान = Five
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0

उदाहरण 2: मान अपडेट करना

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(1, "SUV");
        sortedDict.Add(2, "MUV");
        sortedDict.Add(3, "Utility Vehicle");
        sortedDict.Add(4, "AUV");
        sortedDict.Add(5, "Hatchback");
        sortedDict.Add(6, "Convertible");

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);

        sortedDict.Add(7, "Seven");
        sortedDict.Add(8, "Eight");

        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);

        Console.WriteLine("कुंजी पांच का मान = " + sortedDict[5]);
        sortedDict[5] = "Crossover"; // मान अपडेट करना
        Console.WriteLine("कुंजी पांच का मान (अपडेटेड) = " + sortedDict[5]);

        sortedDict.Clear();
        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
    }
}

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 1, Value = SUV
Key = 2, Value = MUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = AUV
Key = 5, Value = Hatchback
Key = 6, Value = Convertible
SortedDictionary कुंजी-मान जोड़ों की संख्या = 6

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 1, Value = SUV
Key = 2, Value = MUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = AUV
Key = 5, Value = Hatchback
Key = 6, Value = Convertible
Key = 7, Value = Seven
Key = 8, Value = Eight
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 8
कुंजी पांच का मान = Hatchback
कुंजी पांच का मान (अपडेटेड) = Crossover
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0

महत्वपूर्ण व्यवहार

  • Get: यदि कुंजी मौजूद नहीं है तो KeyNotFoundException फेंकता है। सुरक्षित पहुंच के लिए TryGetValue() का उपयोग करें।
  • Set: यदि कुंजी मौजूद है तो मान अपडेट करता है, अन्यथा नया जोड़ता है (Add() के समान लेकिन अपवाद नहीं फेंकता यदि कुंजी मौजूद है)।
  • यही कारण है कि इसे "इंडेक्सर" कहा जाता है - यह ऐरे-जैसी पहुंच सक्षम करता है।
  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