C# | Add a string to the end of the StringCollection
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.
StringCollection.Add(String) Method is used to add a string to the end of the StringCollection.
Syntax:
public int Add (string value);
Here, value is the string to add to the end of the StringCollection. The value can be null.
Return Value: The zero-based index at which the new element is inserted.
Note: If Count is less than the capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.
Below programs illustrate the use of StringCollection.Add(String) Method:
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringCollection myCol = new StringCollection();
myCol.Add( "A" );
myCol.Add( "B" );
myCol.Add( "C" );
myCol.Add( "D" );
myCol.Add( "E" );
foreach (Object obj in myCol)
{
Console.WriteLine(obj);
}
}
}
|
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringCollection myCol = new StringCollection();
myCol.Add( "2" );
myCol.Add( "4" );
myCol.Add( "6" );
myCol.Add( "8" );
myCol.Add( "10" );
foreach (Object obj in myCol)
{
Console.WriteLine(obj);
}
}
}
|
Reference:
Last Updated :
01 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...