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!"); } }}
Updated on: 22-Jun-2020
13K+ Views
- 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?
- Check if a File exists in C#
- 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++
- How do you check a list contains an item in Java?
- 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?
Advertisem*nts