C# | Index of first occurrence in StringCollection Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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.IndexOf(String) method is used to search the specified string which returns the zero-based index of the first occurrence within the StringCollection. Syntax: public int IndexOf (string value); Here, value is the string to locate. The value can be null. Return Value: The method returns zero-based index of the first occurrence of value in the StringCollection, if found otherwise it returns -1. Note: This method performs a linear search. Therefore, this method is an O(n) operation, where n is Count. Below programs illustrate the use of StringCollection.IndexOf(String) method: Example 1: CSHARP // C# code to search string and returns // the zero-based index of the first // occurrence in StringCollection using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // creating a string array named myArr String[] myArr = new String[] { "A", "B", "C", "C", "D" }; // Copying the elements of a string // array to the end of the StringCollection. myCol.AddRange(myArr); // To search string "C" and return // the zero-based index of the first // occurrence in StringCollection // Here, "C" exists at index 2 and 3. // But, the method should return 2 Console.WriteLine(myCol.IndexOf("C")); } } Output: 2 Example 2: CSHARP // C# code to search string and returns // the zero-based index of the first // occurrence in StringCollection using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // creating a string array named myArr String[] myArr = new String[] { "2", "3", "4", "5", "6" }; // Copying the elements of a string // array to the end of the StringCollection. myCol.AddRange(myArr); // To search string "9" and return // the zero-based index of the first // occurrence in StringCollection // Here, "9" does not exist in myCol // Hence, method returns -1 Console.WriteLine(myCol.IndexOf("9")); } } Output: -1 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringcollection.indexof?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Index of first occurrence in StringCollection S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Specialized-Namespace CSharp-Specialized-StringCollection +1 More Similar Reads C# | Remove the first occurrence from 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.Remove(String) method is used to remove the first occurrence of a specific string 3 min read C# | Get or Set at specified index in 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. Properties: StringCollection accepts null as a valid value and allows duplicate elements. String co 2 min read C# | Insert at the specified index in 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.Insert(Int32, String) method is used to insert a string into the StringCollection 2 min read C# | Remove from the specified index 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.RemoveAt(Int32) method is used to remove the string at the specified index of the 3 min read C# | Get the number of strings in 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.Count property is used to get the number of strings contained in the StringCollect 2 min read Like