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

C# में OrderedDictionary में निर्दिष्ट इंडेक्स पर कुंजी और मान डालना

उदाहरण 1: OrderedDictionary में Insert मेथड का उपयोग

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("A", "Books");
        dict.Add("B", "Electronics");
        dict.Add("C", "Smart Wearables");
        dict.Add("D", "Pet Supplies");

        Console.WriteLine("OrderedDictionary तत्व...");
        foreach(DictionaryEntry d in dict) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("OrderedDictionary में तत्वों की संख्या = " + dict.Count);

        dict.Insert(4, "E", "Clothing");
        dict.Insert(5, "F", "Footwear");

        Console.WriteLine("OrderedDictionary तत्व... अपडेटेड");
        foreach(DictionaryEntry d in dict) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("OrderedDictionary में तत्वों की संख्या (अपडेटेड) = " + dict.Count);

        dict.Remove("E");
        Console.WriteLine("OrderedDictionary में तत्वों की संख्या (अपडेटेड) = " + dict.Count);
    }
}

आउटपुट

OrderedDictionary तत्व...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
OrderedDictionary में तत्वों की संख्या = 4
OrderedDictionary तत्व... अपडेटेड
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
OrderedDictionary में तत्वों की संख्या (अपडेटेड) = 6
OrderedDictionary में तत्वों की संख्या (अपडेटेड) = 5

उदाहरण 2: दो OrderedDictionary की तुलना और Insert

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        OrderedDictionary dict1 = new OrderedDictionary();
        dict1.Add("A", "Books");
        dict1.Add("B", "Electronics");
        dict1.Add("C", "Smart Wearables");
        dict1.Add("D", "Pet Supplies");
        dict1.Add("E", "Clothing");
        dict1.Add("F", "Footwear");

        Console.WriteLine("OrderedDictionary1 तत्व...");
        foreach(DictionaryEntry d in dict1) {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        OrderedDictionary dict2 = new OrderedDictionary();
        dict2.Add("1", "One");
        dict2.Add("2", "Two");
        dict2.Add("3", "Three");
        dict2.Add("4", "Four");
        dict2.Add("5", "Five");
        dict2.Add("6", "Six");

        Console.WriteLine("\nOrderedDictionary2 तत्व...");
        foreach(DictionaryEntry d in dict2) {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nक्या OrderedDictionary1, OrderedDictionary2 के बराबर है? = " + (dict1.Equals(dict2)));
        dict2.Insert(3, "10", "Ten");
        Console.WriteLine("\nOrderedDictionary2 तत्व... अपडेटेड");
        foreach(DictionaryEntry d in dict2) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("\nक्या OrderedDictionary1, OrderedDictionary2 के बराबर है? = " + (dict1.Equals(dict2)));
    }
}

आउटपुट

OrderedDictionary1 तत्व...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
OrderedDictionary2 तत्व...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
क्या OrderedDictionary1, OrderedDictionary2 के बराबर है? = False
OrderedDictionary2 तत्व... अपडेटेड
1 One
2 Two
3 Three
10 Ten
4 Four
5 Five
6 Six
क्या OrderedDictionary1, OrderedDictionary2 के बराबर है? = False
  1. - C# में ListDictionary में निर्दिष्ट कुंजी से मान प्राप्त या सेट करें

    C# में ListDictionary वर्ग के सूचकांकर [key] का उपयोग करके निर्दिष्ट कुंजी से जुड़े मान को प्राप्त या सेट किया जा सकता है। यह सूचकांकर कुंजी मौजूद होने पर मान लौटाता है, और मान सेट करने पर मौजूदा मान को अपडेट करता है या नई प्रविष्टि बनाता है। उदाहरण 1: कुंजी से मान प्राप्त करना using System; using

  1. - C# में Hashtable और Dictionary के साथ कार्य करना

    HashtableHashtable क्लास की-वैल्यू पेयर्स के संग्रह को दर्शाता है जो की के हैश कोड के आधार पर व्यवस्थित होते हैं। यह संग्रह में एलिमेंट्स को एक्सेस करने के लिए की का उपयोग करता है।Hashtable क्लास में आमतौर पर उपयोग होने वाले कुछ मेथड्स:क्रमांकमेथड और विवरण1public virtual void Add(object key, object

  1. - Python Pandas - मास्क करें और NaN को विशिष्ट मान से बदलें

    परिचय पैंडास इंडेक्स में NaN मानों को मास्क (पहचान) करके किसी विशिष्ट मान से बदलने के लिए index.putmask() मेथड का उपयोग करें। इसके भीतर index.isna() मेथड सेट करें। आवश्यक लाइब्रेरी आयात करें import pandas as pd import numpy as np कुछ NaN वाला पैंडास इंडेक्स बनाएं index = pd.Index([5, 65, 10, np.na