How to create a StringCollection in C#
StringCollection() constructor is used to initializing a new instance of the StringCollection class. StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace.
Syntax:
public StringCollection ();
Example 1:
// C# Program to illustrate how // to create a StringCollection using System; using System.Collections; using System.Collections.Specialized; class Geeks { // Main Method public static void Main(String[] args) { // sc is the StringCollection object // StringCollection() is the constructor // used to initializes a new // instance of the StringCollection class StringCollection sc = new StringCollection(); // Count property is used to get the // number of elements in StringCollection // It will give 0 as no elements // are present currently Console.WriteLine("Number of elements: "+ sc.Count); } } |
chevron_right
filter_none
Output:
Number of elements: 0
Example 2:
// C# Program to illustrate how // to create a StringCollection using System; using System.Collections; using System.Collections.Specialized; class Geeks { // Main Method public static void Main(String[] args) { // sc is the StringCollection object // StringCollection() is the constructor // used to initializes a new // instance of the StringCollection class StringCollection sc = new StringCollection(); Console.Write("Before Add Method: "); // Count property is used to get the // number of elements in StringCollection // It will give 0 as no elements // are present currently Console.WriteLine(sc.Count); Console.Write("After Add Method: "); // Adding elements in StringCollection sc.Add("A"); sc.Add("B"); sc.Add("C"); sc.Add("D"); sc.Add("E"); // Count property is used to get the // number of elements in ld Console.WriteLine(sc.Count); } } |
chevron_right
filter_none
Output:
Before Add Method: 0 After Add Method: 5
Reference:
Recommended Posts:
- C# | Add a string to the end of the StringCollection
- C# | StringCollection Class
- C# | Get or Set at specified index in StringCollection
- C# | Remove from the specified index of the StringCollection
- How to get Synchronize access to the StringCollection in C#
- C# | Get an enumerator that iterates through StringCollection
- C# | Check if the specified string is in the StringCollection
- C# | Check if the StringCollection is read-only
- C# | Get the number of strings in StringCollection
- C# | Index of first occurrence in StringCollection
- C# | Remove the first occurrence from the StringCollection
- C# | Remove all the strings from the StringCollection
- C# | Insert at the specified index in StringCollection
- C# | Gets or sets the element at the specified index in StringCollection
- C# | Check if two StringCollection objects are equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



