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()से सभी तत्व एक साथ हटा सकते हैं