I have a list
List myList
and I am adding items to a list and I want to check if that object is already in the list.
It depends on the needs of the specific situation. For example, the dictionary approach would be quite good assuming:
If the above are not true for your situation, just use the method Any():
Item wonderIfItsPresent = ...
bool containsItem = myList.Any(item => item.UniqueProperty == wonderIfItsPresent.UniqueProperty);
This will enumerate through the list until it finds a match, or until it reaches the end.
Simple but it works
MyList.Remove(nextObject)
MyList.Add(nextObject)
or
if (!MyList.Contains(nextObject))
MyList.Add(nextObject);
Simply use Contains method. Note that it works based on the equality function Equals
bool alreadyExist = list.Contains(item);