英語で読む

次の方法で共有


Enumerable.Contains メソッド

定義

指定した要素がシーケンスに格納されているかどうかを調べます。

オーバーロード

Contains<TSource>(IEnumerable<TSource>, TSource)

既定の等値比較子を使用して、指定した要素がシーケンスに含まれているかどうかを判断します。

Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>)

指定した IEqualityComparer<T> を使用して、指定した要素がシーケンスに含まれているかどうかを判断します。

Contains<TSource>(IEnumerable<TSource>, TSource)

ソース:
Contains.cs
ソース:
Contains.cs
ソース:
Contains.cs

既定の等値比較子を使用して、指定した要素がシーケンスに含まれているかどうかを判断します。

C#
public static bool Contains<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource value);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

値の検索対象となるシーケンス。

value
TSource

シーケンス内で検索する値。

戻り値

指定した値を持つ要素がソース シーケンスに含まれている場合は true。それ以外は false

例外

sourcenullです。

次のコード例では、 を使用 Contains<TSource>(IEnumerable<TSource>, TSource) して、配列に特定の要素が含まれているかどうかを判断する方法を示します。

C#
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'.

注釈

の型 source が を実装している ICollection<T>場合、 Contains その実装の メソッドが呼び出されて結果が取得されます。 それ以外の場合、このメソッドは、指定した要素が含まれているかどうかを source 判断します。

列挙は、一致する要素が見つかるとすぐに終了します。

要素は、既定の等値比較子 を使用して、 Default指定された値と比較されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>)

ソース:
Contains.cs
ソース:
Contains.cs
ソース:
Contains.cs

指定した IEqualityComparer<T> を使用して、指定した要素がシーケンスに含まれているかどうかを判断します。

C#
public static bool Contains<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource value, System.Collections.Generic.IEqualityComparer<TSource> comparer);
C#
public static bool Contains<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource value, System.Collections.Generic.IEqualityComparer<TSource>? comparer);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

値の検索対象となるシーケンス。

value
TSource

シーケンス内で検索する値。

comparer
IEqualityComparer<TSource>

値を比較する等値比較子。

戻り値

指定した値を持つ要素がソース シーケンスに含まれている場合は true。それ以外は false

例外

sourcenullです。

次の例は、 メソッドで使用できる等価比較子を実装する方法を Contains 示しています。

C#
public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}

// Custom comparer for the Product class
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal.
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal.
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(Product product)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null.
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }
}

この比較子を実装した後、次の例に示すように、 メソッドでContainsオブジェクトのProductシーケンスを使用できます。

C#
Product[] fruits = { new Product { Name = "apple", Code = 9 },
                       new Product { Name = "orange", Code = 4 },
                       new Product { Name = "lemon", Code = 12 } };

Product apple = new Product { Name = "apple", Code = 9 };
Product kiwi = new Product { Name = "kiwi", Code = 8 };

ProductComparer prodc = new ProductComparer();

bool hasApple = fruits.Contains(apple, prodc);
bool hasKiwi = fruits.Contains(kiwi, prodc);

Console.WriteLine("Apple? " + hasApple);
Console.WriteLine("Kiwi? " + hasKiwi);

/*
    This code produces the following output:

    Apple? True
    Kiwi? False
*/

注釈

列挙は、一致する要素が見つかるとすぐに終了します。

nullの場合comparer、要素を指定した値と比較するために、Default既定の等値比較子 が使用されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0