Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Add a string to the end of the StringCollection
Next article icon

C# | Check if the specified string is in the 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.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 :




// 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"));
    }
}
 
 
Output:
  True  

Example 2:




// 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"));
    }
}
 
 
Output:
  False  

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringcollection.contains?view=netframework-4.7.2


Next Article
C# | Add a string to the end of the StringCollection

S

Sahil_Bansall
Improve
Article Tags :
  • C#
  • CSharp-Collections-Namespace
  • CSharp-method
  • CSharp-Specialized-Namespace
  • CSharp-Specialized-StringCollection

Similar Reads

  • 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# | Check if the StringCollection is read-only
    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.IsReadOnly property is used to get a value indicating whether the StringCollection
    1 min read
  • C# | Check if StringCollection is synchronized (thread safe)
    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.IsSynchronized property is used to get a value indicating whether access to the St
    1 min read
  • 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. Syn
    2 min read
  • C# | Check if the StringDictionary contains a specific value
    StringDictionary.ContainsValue(String) method is used to check whether the StringDictionary contains a specific value or not. Syntax: public virtual bool ContainsValue (string value); Here, value is the value to locate in the StringDictionary. The value can be null. Return Value: The method returns
    2 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# | Check if the StringDictionary contains a specific key
    StringDictionary.ContainsKey(String) method is used to check whether the StringDictionary contains a specific key or not. Syntax: public virtual bool ContainsKey (string key); Here, key is the key to locate in the StringDictionary. Return Value: This method returns true if the StringDictionary conta
    2 min read
  • C# | Check if StringDictionary is synchronized (thread safe)
    StringDictionary.IsSynchronized property is used to get a value indicating whether access to the StringDictionary is synchronized (thread-safe). Syntax: public virtual bool IsSynchronized { get; } Return Value: This method returns True if access to the StringDictionary is synchronized (thread safe),
    1 min read
  • 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# | Copy the elements of a string array 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.AddRange(String[]) method is used to copy the elements of a string array to the en
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences