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

C# में OrderedDictionary में कुंजियों को समाहित करने वाला ICollection प्राप्त करें

OrderedDictionary.Keys गुण एक ICollection लौटाता है जिसमें ऑर्डर्ड डिक्शनरी की कुंजियाँ सम्मिलन क्रम में होती हैं। OrderedDictionary कुंजियों के सम्मिलन क्रम को बनाए रखता है।

उदाहरण 1: संख्यात्मक कुंजियाँ

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

public class Demo
{
    public static void Main()
    {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("1", "One");
        dict.Add("2", "Two");
        dict.Add("3", "Three");
        dict.Add("4", "Four");
        dict.Add("5", "Five");
        dict.Add("6", "Six");
        dict.Add("7", "Seven");
        dict.Add("8", "Eight");
        ICollection col = dict.Keys;
        String[] strKeys = new String[dict.Count];
        col.CopyTo(strKeys, 0);
        Console.WriteLine("Displaying the keys...");
        for (int i = 0; i < dict.Count; i++)
        {
            Console.WriteLine(strKeys[i]);
        }
    }
}

आउटपुट

Displaying the keys...
1
2
3
4
5
6
7
8

उदाहरण 2: स्ट्रिंग कुंजियाँ

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

public class Demo
{
    public static void Main()
    {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("One", "John");
        dict.Add("Two", "Tim");
        dict.Add("Three", "Katie");
        dict.Add("Four", "Andy");
        dict.Add("Five", "Gary");
        dict.Add("Six", "Amy");
        ICollection col = dict.Keys;
        String[] strKeys = new String[dict.Count];
        col.CopyTo(strKeys, 0);
        Console.WriteLine("Displaying the keys...");
        for (int i = 0; i < dict.Count; i++)
        {
            Console.WriteLine(strKeys[i]);
        }
    }
}

आउटपुट

Displaying the keys...
One
Two
Three
Four
Five
Six

OrderedDictionary (नामस्थान System.Collections.Specialized) सम्मिलन क्रम बनाए रखता है और कुंजी/इंडेक्स दोनों द्वारा पहुंच की अनुमति देता है। Keys और Values संग्रह लाइव दृश्य हैं। आधुनिक विकल्प: Dictionary<TKey, TValue> (क्रम नहीं) या तृतीय-पक्ष ऑर्डर्ड डिक्शनरी।

  1. - C# में StringDictionary की कुंजियों का संग्रह प्राप्त करें

    परिचय StringDictionary क्लास (System.Collections.Specialized नेमस्पेस में) एक हैश टेबल है जिसमें कुंजी और मान दोनों स्ट्रिंग होते हैं। इसकी Keys प्रॉपर्टी कुंजियों का ICollection लौटाती है, जिसे ऐरे में कॉपी किया जा सकता है या एन्यूमरेट किया जा सकता है। उदाहरण 1: कुंजियों को ऐरे में कॉपी करना using

  1. - C# में Dictionary से कुंजियों (Keys) की सूची प्राप्त करना

    C# में Dictionary<TKey, TValue> से सभी कुंजियों (keys) को एक सूची (List<TKey>) के रूप में प्राप्त करने के लिए Keys प्रॉपर्टी का उपयोग करके कंस्ट्रक्टर या ToList() मेथड का उपयोग किया जाता है। उदाहरण using System; using System.Collections.Generic; public class Demo { public static v

  1. - C# में डिक्शनरी को कीज के अनुसार सॉर्ट कैसे करें?

    C# में HashMap जावा का है। C# में समतुल्य Dictionary<TKey, TValue> है। डिक्शनरी स्वाभाविक रूप से अनसॉर्टेड होती है। कीज के अनुसार सॉर्ट करने के लिए कीज को लिस्ट में कॉपी करके सॉर्ट करें। उदाहरण using System; using System.Collections.Generic; using System.Linq; class Program { static void