Enumerable.Count メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
シーケンス内の要素の数を返します。
オーバーロード
| 名前 | 説明 |
|---|---|
| Count<TSource>(IEnumerable<TSource>) |
シーケンス内の要素の数を返します。 |
| Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
指定したシーケンス内の 1 つの条件を満たす要素の数を表す数値を返します。 |
Count<TSource>(IEnumerable<TSource>)
シーケンス内の要素の数を返します。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static int Count(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static int Count<TSource>(this System.Collections.Generic.IEnumerable<TSource> source);
static member Count : seq<'Source> -> int
<Extension()>
Public Function Count(Of TSource) (source As IEnumerable(Of TSource)) As Integer
型パラメーター
- TSource
sourceの要素の型。
パラメーター
- source
- IEnumerable<TSource>
カウントする要素を含むシーケンス。
返品
入力シーケンス内の要素の数。
例外
source は nullです。
source内の要素の数が Int32.MaxValue より大きい。
例
次のコード例では、 Count<TSource>(IEnumerable<TSource>) を使用して配列内の要素をカウントする方法を示します。
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.
' Create an array of strings.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}
Try
' Count the number of items in the array.
Dim numberOfFruits As Integer = fruits.Count()
' Display the output.
Console.WriteLine($"There are {numberOfFruits} fruits in the collection.")
Catch e As OverflowException
Console.WriteLine("The count is too large to store as an Int32. Try using LongCount() instead.")
End Try
' This code produces the following output:
'
' There are 6 fruits in the collection.
注釈
sourceの型がICollection<T>を実装する場合、その実装は要素の数を取得するために使用されます。 それ以外の場合、このメソッドはカウントを決定します。
LongCountメソッドを使用して、結果をMaxValueより大きくする必要がある場合に使用します。
Visual Basicクエリ式構文では、Aggregate Into Count() 句が Count の呼び出しに変換されます。
こちらもご覧ください
適用対象
Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
指定したシーケンス内の 1 つの条件を満たす要素の数を表す数値を返します。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static int Count(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static int Count<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member Count : seq<'Source> * Func<'Source, bool> -> int
<Extension()>
Public Function Count(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As Integer
型パラメーター
- TSource
sourceの要素の型。
パラメーター
- source
- IEnumerable<TSource>
テストおよびカウントされる要素を含むシーケンス。
返品
述語関数の条件を満たすシーケンス内の要素の数を表す数値。
例外
source または predicate が null。
source内の要素の数が Int32.MaxValue より大きい。
例
次のコード例では、 Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) を使用して、条件を満たす配列内の要素をカウントする方法を示します。
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);
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.
Structure Pet
Public Name As String
Public Vaccinated As Boolean
End Structure
Public Shared Sub CountEx2()
' Create an array of Pet objects.
Dim pets() As Pet = {New Pet With {.Name = "Barley", .Vaccinated = True},
New Pet With {.Name = "Boots", .Vaccinated = False},
New Pet With {.Name = "Whiskers", .Vaccinated = False}}
Try
' Count the number of Pets in the array where the Vaccinated property is False.
Dim numberUnvaccinated As Integer =
pets.Count(Function(p) p.Vaccinated = False)
' Display the output.
Console.WriteLine($"There are {numberUnvaccinated} unvaccinated animals.")
Catch e As OverflowException
Console.WriteLine("The count is too large to store as an Int32. Try using LongCount() instead.")
End Try
End Sub
' This code produces the following output:
'
' There are 2 unvaccinated animals.
注釈
sourceの型がICollection<T>を実装する場合、その実装は要素の数を取得するために使用されます。 それ以外の場合、このメソッドはカウントを決定します。
LongCountメソッドは、予想される場合に使用し、結果がMaxValueより大きいことを許可する必要があります。
Visual Basicクエリ式構文では、Aggregate Into Count() 句が Count の呼び出しに変換されます。