Does the haystack contain the needle inside an array? - CSharp System

CSharp examples for System:Array Search

Description

Does the haystack contain the needle inside an array?

Demo Code


using System.Text;
using System.Collections.Generic;
using System;/*from   ww  w.  j a va  2 s .  c o m*/

public class Main{
        /// <summary>
        /// Does the haystack contain the needle?
        /// </summary>
        /// <param name="haystack"></param>
        /// <param name="needle"></param>
        /// <returns></returns>
        public static bool Contains<Type>(Type[] haystack, Type needle)
            where Type : IComparable
        {
            if (haystack == null)
                return false;

            for (int i = 0; i < haystack.Length; i++)
            {
                if (haystack[i].CompareTo(needle) == 0)
                    return true;
            }

            return false;
        }
}

Related Tutorials