Checks if the string array contains the specified value optionally ignoring case. - CSharp System

CSharp examples for System:Array String

Description

Checks if the string array contains the specified value optionally ignoring case.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;//www  .j  a  v  a 2  s  .c  om

public class Main{
        /// <summary>
        /// Checks if the string array contains the specified value optionally ignoring case.
        /// </summary>
        /// <param name="array">Array to search.</param>
        /// <param name="value">Value to search for.</param>
        /// <param name="comparisonType">Comparison options, e.g. set to <see cref="StringComparison.OrdinalIgnoreCase"/> for a case insensitive comparison.</param>
        /// <returns>True when found.</returns>
        public static bool Contains(this IEnumerable<string> array, string value, StringComparison comparisonType = StringComparison.Ordinal)
        {
            return array.FirstOrDefault(item => String.Compare(item, value, comparisonType) == 0) != null;
        }
        /// <summary>
        /// Searches any array for a value, i.e. without having to create a list or collection.
        /// </summary>
        /// <typeparam name="T">Array type.</typeparam>
        /// <param name="array">Array to search.</param>
        /// <param name="value">Value to find.</param>
        /// <returns>True when present.</returns>
        public static bool Contains<T>(this IEnumerable<T> array, T value)
        {
            return array.Any(item => item.Equals(value));
        }
}

Related Tutorials