C# | Check if the specified string is in 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.Contains(String) method is used to check whether the specified string is in the StringCollection or not.

Syntax:



public bool Contains (string value);

Here, value is the string to locate in the StringCollection. The value can be null.

Return Value: This method returns true if value is found in the StringCollection, otherwise it returns false.

Note:

  • The Contains method can confirm the existence of a string before performing further operations.
  • 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.Contains(String) Method:

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if the specified
// string is in the 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", "D", "E" };
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        // checking if StringCollection
        // Contains "A"
        Console.WriteLine(myCol.Contains("A"));
    }
}

chevron_right


Output:

True

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if the specified
// string is in the 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);
  
        // checking if StringCollection
        // Contains "8"
        Console.WriteLine(myCol.Contains("8"));
    }
}

chevron_right


Output:

False

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.