How to create a StringCollection in C#
Last Updated :
27 Feb, 2019
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:
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main(String[] args)
{
StringCollection sc = new StringCollection();
Console.WriteLine( "Number of elements: " +
sc.Count);
}
}
|
Output:
Number of elements: 0
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main(String[] args)
{
StringCollection sc = new StringCollection();
Console.Write( "Before Add Method: " );
Console.WriteLine(sc.Count);
Console.Write( "After Add Method: " );
sc.Add( "A" );
sc.Add( "B" );
sc.Add( "C" );
sc.Add( "D" );
sc.Add( "E" );
Console.WriteLine(sc.Count);
}
}
|
Output:
Before Add Method: 0
After Add Method: 5
Reference:
Share your thoughts in the comments
Please Login to comment...