Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

StringCollection class is a spcialized collection type present in .NET Framework class libraries. The name itself represents it as a collection of strings. It exposes common methods and properties to manage string collection.

 

If you do not know about this class, in this post we are going learn about it's properties, methods and learn how to use it.

 

Have you heard about StringCollection class in .NET? (www.kunal-chowdhury.com)

 

The class 'StringCollection' is defined in the 'System.Collections.Specialized' namespace of the .NET Framework class library. It implements IList, ICollection and IEnumerable interfaces to provide common set of collection APIs.

 

Here is the definition of the 'StringCollection' class, that exposes few additional properties and methods to perform add/remove operations to it:

 

namespace System.Collections.Specialized
{
    public class StringCollection : IList, ICollection, IEnumerable
    {
        public StringCollection();
        public string this[int index] { get; set; }
 
        #region Public Properties
        public int Count { get; }
        public bool IsReadOnly { get; }
        public bool IsSynchronized { get; }
        public object SyncRoot { get; }
        #endregion Public Properties
 
        #region Public Methods
        public int Add(string value);
        public void AddRange(string[] value);
        public void Clear();
        public bool Contains(string value);
        public void CopyTo(string[] array, int index);
        public StringEnumerator GetEnumerator();
        public int IndexOf(string value);
        public void Insert(int index, string value);
        public void Remove(string value);
        public void RemoveAt(int index);
        #endregion Public Methods
    }
}

 

 

Just like other collections, you can perform add/insert to this collection type. For an addition of a single element, you can call the 'Add' method; but to perform addition an entire string array, you can call the 'AddRange' method. You can also call 'Insert' method to insert any string element at a specific index position of the collection.

 

To remove an element from the collection, you can either call the Remove/RemoveAt method. The 'Remove' method removes the first occurance of the given string, whereas the 'RemoveAt' method removes a string specified at the given index location. To remove all the elements from the collection, call the 'Clear' method.

 

 

Here is a code snippet of the simple examples to perform basic operations like add/insert/remove/clear on the StringCollection class:

 

var stringCollection = new StringCollection();
 
stringCollection.Add("1");
stringCollection.Add("2");
stringCollection.Add("3");
PrintValues(stringCollection);
 
var stringArray = new string[] { "4", "5", "6" };
stringCollection.AddRange(stringArray);
PrintValues(stringCollection);
 
stringCollection.Remove("4");
PrintValues(stringCollection);
 
stringCollection.Insert(3, "4");
PrintValues(stringCollection);
 
stringCollection.Clear();
PrintValues(stringCollection);
 
public static void PrintValues(StringCollection stringCollection)
{
    Console.WriteLine();
    Console.WriteLine();
    foreach (var stringValue in stringCollection)
    {
        Console.Write(stringValue + " ");
    }
}

 

I hope that the post was clear and easy to understand. Did you learn something new today? Do share with us. Sharing is caring. So, don't forget to share this post in your channel and help your friend or colleague to learn something new.

 

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.