Add item to IList If Not Exist - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Add item to IList If Not Exist

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from ww  w.j a va  2 s.com

public class Main{
        public static void AddIfNotExist<T>(this ICollection<T> collection, T item)
        {
            if (!collection.Contains(item))
                collection.Add(item);
        }
}

Related Tutorials