英語で読む

次の方法で共有


Enumerable.Count メソッド

定義

シーケンス内の要素数を返します。

オーバーロード

Count<TSource>(IEnumerable<TSource>)

シーケンス内の要素数を返します。

Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

条件を満たす、指定されたシーケンス内の要素の数を表す数値を返します。

Count<TSource>(IEnumerable<TSource>)

ソース:
Count.cs
ソース:
Count.cs
ソース:
Count.cs

シーケンス内の要素数を返します。

C#
public static int Count<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

カウントする要素が格納されているシーケンス。

戻り値

入力シーケンス内の要素数。

例外

sourcenullです。

source の要素の数が Int32.MaxValue を超えています。

次のコード例では、 を使用 Count<TSource>(IEnumerable<TSource>) して配列内の要素をカウントする方法を示します。

C#
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

try
{
    int numberOfFruits = fruits.Count();
    Console.WriteLine(
        "There are {0} fruits in the collection.",
        numberOfFruits);
}
catch (OverflowException)
{
    Console.WriteLine("The count is too large to store as an Int32.");
    Console.WriteLine("Try using the LongCount() method instead.");
}

// This code produces the following output:
//
// There are 6 fruits in the collection.

注釈

の型 source が を実装する ICollection<T>場合、その実装は要素の数を取得するために使用されます。 それ以外の場合、このメソッドはカウントを決定します。

を予期し、 LongCount 結果が より MaxValue大きいことを許可する場合は、 メソッドを使用します。

Visual Basic クエリ式の構文では、 句は Aggregate Into Count()Count呼び出しに変換されます。

こちらもご覧ください

適用対象

.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

Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

ソース:
Count.cs
ソース:
Count.cs
ソース:
Count.cs

条件を満たす、指定されたシーケンス内の要素の数を表す数値を返します。

C#
public static int Count<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

テストおよびカウントする要素が格納されているシーケンス。

predicate
Func<TSource,Boolean>

各要素が条件を満たしているかどうかをテストする関数。

戻り値

述語関数の条件を満たす、シーケンス内の要素数を表す数値。

例外

source または predicatenull です。

source の要素の数が Int32.MaxValue を超えています。

次のコード例では、 を使用 Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) して、条件を満たす配列内の要素をカウントする方法を示します。

C#
class Pet
{
    public string Name { get; set; }
    public bool Vaccinated { get; set; }
}

public static void CountEx2()
{
    Pet[] pets = { new Pet { Name="Barley", Vaccinated=true },
                   new Pet { Name="Boots", Vaccinated=false },
                   new Pet { Name="Whiskers", Vaccinated=false } };

    try
    {
        int numberUnvaccinated = pets.Count(p => p.Vaccinated == false);
        Console.WriteLine("There are {0} unvaccinated animals.", numberUnvaccinated);
    }
    catch (OverflowException)
    {
        Console.WriteLine("The count is too large to store as an Int32.");
        Console.WriteLine("Try using the LongCount() method instead.");
    }
}

// This code produces the following output:
//
// There are 2 unvaccinated animals.

注釈

の型 source が を実装する ICollection<T>場合、その実装は要素の数を取得するために使用されます。 それ以外の場合、このメソッドはカウントを決定します。

を想定し、結果が LongCount より MaxValue大きいことを許可する場合は、 メソッドを使用する必要があります。

Visual Basic クエリ式の構文では、 句は Aggregate Into Count()Count呼び出しに変換されます。

こちらもご覧ください

適用対象

.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