Remove all the strings from the StringCollection in C#

The StringCollection class in C# provides the Clear() method to remove all strings from the collection at once. This method is part of the System.Collections.Specialized namespace and is useful when you need to empty the entire collection efficiently.

Syntax

Following is the syntax for the Clear() method −

stringCollection.Clear();

Parameters

The Clear() method does not take any parameters.

Return Value

The Clear() method does not return any value. It has a return type of void.

StringCollection.Clear() Process Before Clear() "A", "B", "C" Count = 3 After Clear() (empty) Count = 0 Clear() All strings are removed in a single operation

Using Clear() with Numeric Strings

Example

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);
        
        stringCol.Clear();
        Console.WriteLine("\nWe have removed all the elements now..");
        Console.WriteLine("Total number of elements now = " + stringCol.Count);
    }
}

The output of the above code is −

Array elements...
100
200
300
400
500
Does the specified string is in the StringCollection? = False
Total number of elements = 5

We have removed all the elements now..
Total number of elements now = 0

Using Clear() with Character Strings

Example

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", "F", "G", "H", "I", "J" };
        
        Console.WriteLine("Array elements...");
        foreach (string res in arr) {
            Console.WriteLine(res);
        }
        
        stringCol.AddRange(arr);
        Console.WriteLine("Count of elements = " + stringCol.Count);
        
        stringCol.Clear();
        Console.WriteLine("\nRemoved all the elements now...");
        Console.WriteLine("Total number of elements now = " + stringCol.Count);
    }
}

The output of the above code is −

Array elements...
A
B
C
D
E
F
G
H
I
J
Count of elements = 10

Removed all the elements now...
Total number of elements now = 0

Key Points

  • The Clear() method removes all elements from the StringCollection in a single operation.

  • After calling Clear(), the Count property returns 0.

  • This method is more efficient than removing elements one by one using Remove() or RemoveAt().

  • The StringCollection remains usable after clearing − you can add new elements to it.

Conclusion

The Clear() method provides an efficient way to remove all strings from a StringCollection at once. It sets the count to zero and makes the collection ready for new elements, making it ideal for scenarios where you need to reset the collection's contents.

Updated on: 2026-03-17T07:04:36+05:30

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements