In this source code example, we will demonstrate how to check whether the element is in the List in C#.
The Contains method determines whether the element is in the list.
C# List Contains example
In the example, we check if the oak word is in the list.using System;
using System.Collections.Generic;
public class ListDemo
{
public static void Main(string[] args)
{
var fruits = new List<string> { "banana", "mango", "apple", "watermelon" };
if (fruits.Contains("apple"))
{
Console.WriteLine("The list contains the apple fruit");
}
}
}
Output:
The list contains the apple fruit
Related C# List Examples
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course