
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to check if an item exists in a C# list collection?
Set a list −
List < string > list1 = new List < string > () { "Lawrence", "Adams", "Pitt", "Tom" };
Now use the Contains method to check if an item exits in a list or not.
if (list1.Contains("Adams") == true) { Console.WriteLine("Item exists!"); }
The following is the code to check if an item exists in a C# list or not.
Example
using System; using System.Collections.Generic; public class Program { public static void Main() { List < string > list1 = new List < string > () { "Lawrence", "Adams", "Pitt", "Tom" }; Console.Write("List...
"); foreach(string list in list1) { Console.WriteLine(list); } Console.Write("Finding an item in the list...
"); if (list1.Contains("Adams") == true) { Console.WriteLine("Item exists!"); } else { Console.WriteLine("Item does not exist!"); } } }
- Related Articles
- How to check if an item exists in a C# array?
- How to check if a vector exists in a list in R?
- How do you check a list contains an item in Java?
- Check if a list exists in given list of lists in Python
- Check if a pair with given product exists in Linked list in C++
- Check if a File exists in C#
- How to check if ArrayList contains an item in Java?
- How to check if an alert exists using WebDriver?
- How to Check if a Key Exists in the Hashtable in C#?
- How to check if Element exists in c# Selenium drivers?
- Check if value exists in a comma separated list in MySQL?
- How to check if a variable exists in JavaScript?
- How to check if a column exists in Pandas?
- How to check if a file exists in Golang?
- How to check if a file exists in Perl?

Advertisements