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

C# में StringCollection से निर्दिष्ट सूचकांक पर तत्व हटाएं

C# में StringCollection वर्ग की RemoveAt(int index) विधि का उपयोग करके निर्दिष्ट सूचकांक पर स्थित स्ट्रिंग को हटाया जा सकता है। यह विधि शून्य-आधारित सूचकांक स्वीकार करती है और तत्व हटाने के बाद शेष तत्वों को स्वचालित रूप से शिफ्ट कर देती है।

उदाहरण 1: सूचकांक 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("सरणी तत्व...");
        foreach (string res in arr) {
            Console.WriteLine(res);
        }
        
        stringCol.AddRange(arr);
        Console.WriteLine("कुल तत्वों की संख्या = " + stringCol.Count);
        
        stringCol.RemoveAt(3);
        
        Console.WriteLine("कुल तत्वों की संख्या अब = " + stringCol.Count);
    }
}

आउटपुट

सरणी तत्व...
100
200
300
400
500
कुल तत्वों की संख्या = 5
कुल तत्वों की संख्या अब = 4

उदाहरण 2: एकाधिक सूचकांकों पर तत्व हटाना

using System;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringCollection stringCol = new StringCollection();
        String[] arr = new String[] { "A", "B", "C", "D", "E" };
        
        Console.WriteLine("सरणी तत्व...");
        foreach (string res in arr) {
            Console.WriteLine(res);
        }
        
        stringCol.AddRange(arr);
        Console.WriteLine("कुल तत्वों की संख्या = " + stringCol.Count);
        
        stringCol.RemoveAt(1);
        stringCol.RemoveAt(2);
        
        Console.WriteLine("कुल तत्वों की संख्या अब = " + stringCol.Count);
    }
}

आउटपुट

सरणी तत्व...
A
B
C
D
E
कुल तत्वों की संख्या = 5
कुल तत्वों की संख्या अब = 3

मुख्य बिंदु

  • RemoveAt(index) शून्य-आधारित सूचकांक पर तत्व हटाता है
  • अमान्य सूचकांक पर ArgumentOutOfRangeException फेंकता है
  • पहले RemoveAt(1) से "B" हटता है, फिर RemoveAt(2) नई सूची में "D" हटाता है
  • Remove(string) से मान द्वारा पहली घटना हटा सकते हैं
  • Clear() से सभी तत्व एक साथ हटा सकते हैं
  1. - C# में SortedSet से सभी तत्व हटाएं

    उदाहरण SortedSet<T> की Clear() मेथड सेट से सभी तत्वों को हटा देती है। इस उदाहरण में दो SortedSet बनाए गए हैं, दूसरे में तत्व जोड़े गए हैं, फिर Clear() कॉल करके खाली किया गया है। using System; using System.Collections.Generic; public class Demo { public static void Main() { SortedS

  1. - C# में ArrayList से तत्वों की एक रेंज हटाएं

    उदाहरण ArrayList की RemoveRange() मेथड निर्दिष्ट इंडेक्स से शुरू होने वाले निर्दिष्ट संख्या में तत्वों को हटा देती है। इस उदाहरण में इंडेक्स 3 से 2 तत्व हटाए गए हैं। using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list1 = ne

  1. - C# में ArrayList से सभी तत्व हटाएं

    उदाहरण ArrayList की Clear() मेथड सूची से सभी तत्वों को हटा देती है। इस उदाहरण में तीन ArrayList बनाए गए हैं, तीसरे को दूसरे के बराबर सेट किया गया है, फिर Clear() कॉल करके खाली किया गया है। using System; using System.Collections; public class Demo { public static void Main(String[] args) {