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

C# में संग्रह में निर्दिष्ट ऑब्जेक्ट के इंडेक्स की खोज


संग्रह में निर्दिष्ट ऑब्जेक्ट के इंडेक्स की खोज करने के लिए, कोड निम्नानुसार है:

उदाहरण

using System; 
using System.Collections.Specialized; 
public class Demo { 
    public static void Main() { 
        StringCollection strCol = new StringCollection(); 
        strCol.Add("Accessories"); 
        strCol.Add("Books"); 
        strCol.Add("Electronics"); 
        strCol.Add("Books"); 
        Console.WriteLine("StringCollection elements..."); 
        foreach (string res in strCol) { 
            Console.WriteLine(res); 
        } 
        strCol.Insert(2, "Headphone"); 
        Console.WriteLine("StringCollection elements...UPDATED"); 
        foreach (string res in strCol) { 
            Console.WriteLine(res); 
        } 
        Console.WriteLine("Index of specific object Electronics? = " + strCol.IndexOf("Electronics")); 
    } 
}

आउटपुट

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

StringCollection elements... 
Accessories 
Books 
Electronics 
Books 
StringCollection elements...UPDATED 
Accessories 
Books 
Headphone 
Electronics 
Books 
Index of specific object Electronics? = 3

उदाहरण

आइए अब एक और उदाहरण देखें:

using System; 
using System.Collections.Specialized; 
public class Demo { 
    public static void Main() { 
        StringCollection stringCol = new StringCollection(); 
        String[] arr = new String[] { "100", "200", "300", "400", "500" }; 
        Console.WriteLine("Array elements..."); 
        foreach (string res in arr) { 
            Console.WriteLine(res); 
        } 
        stringCol.AddRange(arr); 
        Console.WriteLine("Does the specified string is in the StringCollection? = " + stringCol.Contains("800")); 
        Console.WriteLine("Total number of elements = " + stringCol.Count); 
        Console.WriteLine("Iterating through StringCollection = " + stringCol.Count); 
        StringEnumerator myenum = stringCol.GetEnumerator(); 
        while (myenum.MoveNext()) 
            Console.WriteLine(myenum.Current); 
        Console.WriteLine("Index of specific object 500? = " + stringCol.IndexOf("500")); 
        Console.WriteLine("Index of specific object 1000? = " + stringCol.IndexOf("1000")); 
    } 
}

आउटपुट

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

Array elements... 
100 
200 
300 
400 
500 
Does the specified string is in the StringCollection? = False 
Total number of elements = 5 
Iterating through StringCollection = 5 
100 
200 
300 
400 
500 
Index of specific object 500? = 4 
Index of specific object 1000? = -1

  1. - C# में Collection से निर्दिष्ट इंडेक्स पर तत्व हटाएं

    उदाहरण C# में Collection<T> क्लास का उपयोग करके किसी विशिष्ट इंडेक्स पर मौजूद तत्व को हटाने के लिए RemoveAt() मेथड का प्रयोग किया जाता है। नीचे दिए गए उदाहरण में एक स्ट्रिंग कलेक्शन बनाया गया है, उसमें कुछ नाम जोड़े गए हैं, और फिर इंडेक्स 3 पर मौजूद तत्व को हटा दिया गया है। using System; using

  1. - C# में SortedSet ऑब्जेक्ट उचित सुपरसेट है या नहीं जांचें

    C# में एक SortedSet ऑब्जेक्ट निर्दिष्ट संग्रह का उचित सुपरसेट है या नहीं, यह जांचने के लिए IsProperSupersetOf() मेथड का उपयोग किया जाता है। यह मेथड true लौटाता है यदि सेट निर्दिष्ट संग्रह के सभी तत्वों को रखता है और कम से कम एक अतिरिक्त तत्व भी रखता है। उदाहरण 1 using System; using System.Collection

  1. - C# में SortedSet ऑब्जेक्ट उचित सबसेट है या नहीं जांचें

    C# में एक SortedSet ऑब्जेक्ट निर्दिष्ट संग्रह का उचित सबसेट है या नहीं, यह जांचने के लिए IsProperSubsetOf() मेथड का उपयोग किया जाता है। यह मेथड true लौटाता है यदि सेट के सभी तत्व निर्दिष्ट संग्रह में मौजूद हैं और निर्दिष्ट संग्रह में कम से कम एक अतिरिक्त तत्व है। उदाहरण 1 using System; using System.