C# Enumerable Contains(IEnumerable, TSource)
Description
Determines whether a sequence contains a specified element by using the default equality comparer.
Syntax
public static bool Contains<TSource>(
this IEnumerable<TSource> source,
TSource value
)
Parameters
TSource
- The type of the elements of source.source
- A sequence in which to locate a value.value
- The value to locate in the sequence.
Returns
returns true if the source sequence contains an element that has the specified value; otherwise, false.
Example
The following code example demonstrates how to use Contains(IEnumerable, TSource) to determine whether an array contains a specific element.
/* w ww .ja v a 2s . c o m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };
string fruit = "mango";
bool hasMango = fruits.Contains(fruit);
Console.WriteLine(hasMango);
}
}
The code above generates the following result.