Add Unique value to ICollection - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Add Unique value to ICollection

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;/*from w  w w .  j a va 2 s .co  m*/

public class Main{

      public static void AddUnique<T>(this ICollection<T> items, T item)
      {
         if (!items.Contains(item))
         {
            items.Add(item);
         }
      }
    public static bool Contains<T>(this IEnumerable<T> list, Func<T, bool> predicate)
      {
         foreach (var item in list)
         {
            if (predicate(item))
            {
               return true;
            }
         }
         return false;
      }
}

Related Tutorials