find and Remove from IList

If the User objects you use are held within the _Players list (same object references) you can just do

_Players.Remove(user);

Otherwise if only the id matches you can do:

_Players.RemoveAll( p => p.ID == user.ID);

How about this? Use this if your parameter User is not part of _Players.

 _Players.Remove(_Players.SingleOrDefault(x => x.ID == User.ID));

The SingleOrDefault() ensures that if the match is not found, that null is returned. When trying to remove null, no err occurs or is thrown.


There are two scenarios, the user variable that you are passing to RemovePlayer

  1. is actually contained in your list
  2. has the same ID value, but is not the same object.

From your code sample it is impossible to say.

For the first case just call _Players.Remove(user). For the second case implement the System.IEquatable<User> interface on User to define a default EqualityComparer<User> and then again call _Players.Remove(user). This second scenario works in both cases.