Skip to content

Instantly share code, notes, and snippets.

@AverageMarcus
Last active December 15, 2015 00:49
Show Gist options
  • Save AverageMarcus/5176008 to your computer and use it in GitHub Desktop.
Save AverageMarcus/5176008 to your computer and use it in GitHub Desktop.
An extension to ICollection to provide a .AddIfNotExists() method to remove the need for constant checking.
/// <summary>
/// Adds an item to the list only if it doesn't already exist there.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="coll"></param>
/// <param name="item"></param>
public static void AddIfNotExists<T>(this ICollection<T> coll, T item) {
if (!coll.Contains(item))
{
coll.Add(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment