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.
Properties:
- StringCollection accepts null as a valid value and allows duplicate elements.
- String comparisons are case-sensitive.
- Elements in this collection can be accessed using an integer index.
- Indexes in this collection are zero-based.
Below programs illustrate how to get or set the elements at the specified index in StringCollection:
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "G" , "e" , "E" , "k" , "s" };
myCol.AddRange(myArr);
Console.WriteLine(myCol[2]);
}
}
|
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "3" , "5" , "7" , "11" , "13" };
myCol.AddRange(myArr);
Console.WriteLine(myCol[3]);
myCol[3] = "8" ;
Console.WriteLine(myCol[3]);
}
}
|