You can use the method List.Contains().
Determines if an item is in List<T>.
how
List<int> list = new List<int>(){1, 2, 3, 4, 5};
if(!list.Contains(6))
list.Add(6);
foreach (var i in list)
{
Console.WriteLine(i);
}
The output will be:
1
2
3
4
5
6
Here . demonstration
source
share