Last active
December 15, 2015 00:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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