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

C# में ListDictionary में कुंजियाँ प्राप्त करने के लिए ICollection प्राप्त करें

C# में ListDictionary वर्ग की सभी कुंजियों को ICollection के रूप में प्राप्त करने के लिए Keys प्रॉपर्टी का उपयोग किया जाता है। यह प्रॉपर्टी डिक्शनरी में मौजूद सभी कुंजियों का संग्रह लौटाती है, जिसे आप foreach लूप से पुनरावृत्त कर सकते हैं।

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

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

public class Demo {
    public static void Main() {
        ListDictionary listDict = new ListDictionary();
        listDict.Add("1", "Laptop");
        listDict.Add("2", "Tablet");
        listDict.Add("3", "Desktop");
        listDict.Add("4", "Speaker");
        listDict.Add("5", "Earphone");
        listDict.Add("6", "Headphone");
        
        ICollection col = listDict.Keys;
        
        foreach(String s in col) {
            Console.WriteLine(s);
        }
    }
}

आउटपुट

1
2
3
4
5
6

उदाहरण 2: अक्षर कुंजियाँ

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

public class Demo {
    public static void Main() {
        ListDictionary listDict = new ListDictionary();
        listDict.Add("A", "Tom");
        listDict.Add("B", "John");
        listDict.Add("C", "Kevin");
        listDict.Add("D", "Tim");
        
        ICollection col = listDict.Keys;
        
        foreach(String s in col) {
            Console.WriteLine(s);
        }
    }
}

आउटपुट

A
B
C
D

मुख्य बिंदु

  • ListDictionary.Keys प्रॉपर्टी ICollection लौटाती है
  • foreach लूप से कुंजियों को आसानी से पुनरावृत्त कर सकते हैं
  • ListDictionary छोटे डेटा सेट के लिए अनुकूलित है
  1. - C# में SortedList Class की Keys Property क्या है?

    SortedList में Keys को SortedList Class की Keys Property का उपयोग करके Get करें। हमने पहले SortedList Property को Elements के साथ Set किया है।SortedList sl = new SortedList(); sl.Add(ST0, One); sl.Add(ST1, Two); sl.Add(ST2, Three); sl.Add(ST3, Four); sl.Add(ST4, Five); sl.Add(ST5, Six); sl.Add(ST6,

  1. - C# में List.GetRange() से तत्वों की रेंज प्राप्त करना

    C# में List<T>.GetRange() मेथड का उपयोग करके सूची के एक सन्निकट (contiguous) भाग को एक नई सूची के रूप में प्राप्त किया जा सकता है। यह ArraySegment या LINQ के Skip()/Take() के समान है लेकिन सूची-विशिष्ट है। उदाहरण using System; using System.Collections.Generic; public class Demo { public

  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