Have you heard about StringCollection class in .NET?

Have you heard about StringCollection class in .NET?


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.

 

 



If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

8 comments

  1. What exactly is special about this collection? What more does it offer compared to List?

    ReplyDelete
    Replies
    1. Nothing much, but just brushing up some old APIs which more or less people don't know. Though not necessary, but might be useful for someone... :)

      Delete
  2. StringCollection is a type originating in the .NET Framework Version 1. At this point in time Generics did not exist, so List(Of String) did not exist. Microsoft thought, it would be a good idea to have some specialized Collections. System.Windows.Forms is full of specialized collections that would otherwise have been implemented with simple Generics.

    ReplyDelete
  3. Is there a reason to use this in 2017?

    ReplyDelete
  4. Doesn't seem like there is a point to this when we have generics today.

    ReplyDelete
    Replies
    1. Totally agree with you all, but there's no end to learn and bookmark something new. Of course, I use generics, but brushing up some basics is also important in my choice.

      Hopefully, someone may find it interesting to learn/know. :)

      Delete
  5. I'm not sure there's a good reason to use StringCollection over List...
    However -- in that same namespace there's the equally forgotten StringDictionary which conveniently return null when referencing a non-existing index (i.e., mydict["notthere"]) Dictionary throws an exception in that case, which pretty much makes the brackets operator unusable.
    With StringDictionary you can write:
    return mydict[key] ?? defaultValue;

    With Dictionary, you'd have to write:
    try {
    return mydict[key];
    }
    catch
    {
    return defaultValue;
    }

    ReplyDelete
    Replies
    1. in C# 7 you can write: if(mydict.TryGetValue(key, out MyValue myvalue)) { ... }

      Delete

 
© 2008-2017 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top