C# | StringCollection Class
Last Updated :
24 Jan, 2019
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.
Characteristics:
- StringCollection class 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.
Constructors
Constructor |
Description |
StringCollection() |
Initializes a new instance of the StringCollection class. |
Example:
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);
}
}
}
|
Output:
A
B
C
D
E
Properties
Property |
Description |
Count |
Gets the number of strings contained in the StringCollection. |
IsReadOnly |
Gets a value indicating whether the StringCollection is read-only. |
IsSynchronized |
Gets a value indicating whether access to the StringCollection is synchronized (thread safe). |
Item[Int32] |
Gets or sets the element at the specified index. |
SyncRoot |
Gets an object that can be used to synchronize access to the StringCollection. |
Example:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "A" , "B" , "C" , "D" , "E" };
myCol.AddRange(myArr);
Console.WriteLine(myCol.IsReadOnly);
Console.WriteLine( "Number of strings in myCol are : "
+ myCol.Count);
}
}
|
Output:
False
Number of strings in myCol are : 5
Methods
Method |
Description |
Add(String) |
Adds a string to the end of the StringCollection. |
AddRange(String[]) |
Copies the elements of a string array to the end of the StringCollection. |
Clear() |
Removes all the strings from the StringCollection. |
Contains(String) |
Determines whether the specified string is in the StringCollection. |
CopyTo(String[], Int32) |
Copies the entire StringCollection values to a one-dimensional array of strings, starting at the specified index of the target array. |
Equals(Object) |
Determines whether the specified object is equal to the current object. |
GetEnumerator() |
Returns a StringEnumerator that iterates through the StringCollection. |
GetHashCode() |
Serves as the default hash function. |
GetType() |
Gets the Type of the current instance. |
IndexOf(String) |
Searches for the specified string and returns the zero-based index of the first occurrence within the StringCollection. |
Insert(Int32, String) |
Inserts a string into the StringCollection at the specified index. |
MemberwiseClone() |
Creates a shallow copy of the current Object. |
Remove(String) |
Removes the first occurrence of a specific string from the StringCollection. |
RemoveAt(Int32) |
Removes the string at the specified index of the StringCollection. |
ToString() |
Returns a string that represents the current object. |
Example:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringCollection myCol = new StringCollection();
String[] myArr1 = new String[] { "A" , "B" , "C" , "D" , "E" };
myCol.AddRange(myArr1);
String[] myArr2 = new String[myCol.Count];
myCol.CopyTo(myArr2, 0);
for ( int i = 0; i < myArr2.Length; i++) {
Console.WriteLine(myArr2[i]);
}
}
}
|
Output:
A
B
C
D
E
Example:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "Hello" , "Geeks" ,
"for" , "GeeksforGeeks" };
myCol.AddRange(myArr);
Console.WriteLine( "Initially elements in StringCollection are: " );
foreach (Object obj in myCol)
Console.WriteLine(obj);
myCol.Clear();
Console.WriteLine( "After Removing: " );
foreach (Object obj in myCol)
Console.WriteLine(obj);
}
}
|
Output:
Initially elements in StringCollection are:
Hello
Geeks
for
GeeksforGeeks
After Removing:
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...