Enumerable.Contains 方法 (System.Linq)

您所在的位置:网站首页 array_contains多值 Enumerable.Contains 方法 (System.Linq)

Enumerable.Contains 方法 (System.Linq)

2024-06-16 20:37| 来源: 网络整理| 查看: 265

Source:Contains.cs Source:Contains.cs Source:Contains.cs

通过使用默认的相等比较器确定序列是否包含指定的元素。

public: generic [System::Runtime::CompilerServices::Extension] static bool Contains(System::Collections::Generic::IEnumerable ^ source, TSource value); public static bool Contains (this System.Collections.Generic.IEnumerable source, TSource value); static member Contains : seq bool Public Function Contains(Of TSource) (source As IEnumerable(Of TSource), value As TSource) As Boolean 类型参数 TSource

source 的元素类型。

参数 source IEnumerable

要在其中定位某个值的序列。

value TSource

要在序列中定位的值。

返回 Boolean

如果源序列包含具有指定值的元素,则为 true;否则为 false。

例外 ArgumentNullException

source 为 null。

示例

下面的代码示例演示如何使用 Contains(IEnumerable, TSource) 来确定数组是否包含特定元素。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" }; string fruit = "mango"; bool hasMango = fruits.Contains(fruit); Console.WriteLine( "The array {0} contain '{1}'.", hasMango ? "does" : "does not", fruit); // This code produces the following output: // // The array does contain 'mango'. ' Create an array of strings. Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"} ' This is the string to search the array for. Dim fruit As String = "mango" ' Determine if the array contains the specified string. Dim hasMango As Boolean = fruits.Contains(fruit) Dim text As String = IIf(hasMango, "does", "does not") ' Display the output. Console.WriteLine($"The array {text} contain {fruit}") ' This code produces the following output: ' ' The array does contain mango 注解

如果 的类型 source 实现 ICollection, Contains 则调用该实现中的 方法来获取结果。 否则,此方法确定是否 source 包含指定的元素。

一旦找到匹配的元素,枚举就会终止。

使用默认相等比较器 Default将元素与指定的值进行比较。

适用于


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3