Determines whether the ICollection contains the specified Object. - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Determines whether the ICollection contains the specified Object.

Demo Code

// Copyright ? Stateside Technology Limited.  All rights reserved.
using System.Reflection;
using System.Collections;
using System;// www .j  a v a  2  s  . c  o  m

public class Main{
        /// <summary>
      /// Determines whether the <paramref name="collection"/> contains the specified <paramref name="element"/>.
      /// </summary>
      /// <param name="collection">The collection to check.</param>
      /// <param name="element">The object to locate in the collection.</param>
      /// <returns><see lang="true"/> if the element is in the collection, <see lang="false"/> otherwise.</returns>
      public static bool Contains(ICollection collection, Object element)
      {
         if (collection == null)
         {
            throw new ArgumentNullException("Collection cannot be null.");
         }
         MethodInfo method;
         method = collection.GetType().GetMethod("contains", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
         if (null == method)
         {
            throw new InvalidOperationException("Collection type " + collection.GetType() + " does not implement a Contains() method.");
         }
         return (bool) method.Invoke(collection, new Object[] {element});
      }
}

Related Tutorials