Contains method checks whether item is present in the give List if found then returns True otherwise returns False.
For Example: List<string> lst_names = new List<string>();
lst_names.Add("Rajesh");
lst_names.Add("Kanchan");
lst_names.Add("Vindu");
lst_names.Add("Ravi");
lst_names.Add("Parash");
if (lst_names.Contains("Vindu"))
{
Response.Write("Vindu is present in the List collection");
}
//Using StringComparer.OrdinalIgnoreCase check for case-sensitiveness
if (lst_names.Contains("RAJESH", StringComparer.OrdinalIgnoreCase))
{
Response.Write("Yes Rajesh is also present in the List collection");
}
Response.Write(lst_names.Contains("Kumar"));
Output:
Vindu is present in the List collection
Yes Rajesh is also present in the List collection
False