C# List.Contains() strategy: Here, we will find out about the Contains() technique for List with example.
C# List.Contains() Method
List.Contains() strategy is utilized to check whether rundown contains a predetermined component or not.
Syntax:
bool List<T>.Contains(T item);
Parameter: It acknowledges a thing of type T.
Return value: It restores a Boolean value. genuine if the list contains the thing, false if the list doesn’t contain the thing.
Example:
int list declaration:
List<int> a = new List<int>();
adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
checking an item:
a.Contains(10)
Output:
true
C# Example to check a thing in the rundown utilizing List.Contains() Method:
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void printList(List<int> lst)
{
//printing elements
foreach (int item in lst)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//integer list
List<int> a = new List<int>();
//adding elements
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//print the list
Console.WriteLine("list elements...");
printList(a);
//finding elements
if (a.Contains(10))
Console.WriteLine("List contains 10");
else
Console.WriteLine("List does not contain 10");
if (a.Contains(60))
Console.WriteLine("List contains 60");
else
Console.WriteLine("List does not contain 60");
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output:
list elements...
10 20 30 40 50
List contains 10
List does not contain 60