You can use List<T>.Contains
- just be aware that it will be a linear search, i.e. O(N) instead of the O(1) of a HashMap
. If your list isn't too large, it's unlikely to be much of a problem. Of course, you still need the items to override Equals
appropriately unless you're happy with reference identity.
If you've got a large list which you'll need to do repeated containment tests on, you may just want to create a HashSet<T>
from the existing list. If you're going to be manipulating the list a lot as you go, you may want to encapsulate both the list and the set together in your own collection. You'll need to work out which semantics you want though - what would you want to happen if you added the same ID twice? Should the second call be ignored? If you can get away without doing this, so much the better :)