Enumerable.Count 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回序列中的項目數目。
多載
| 名稱 | Description |
|---|---|
| Count<TSource>(IEnumerable<TSource>) |
傳回序列中的項目數目。 |
| Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
傳回數位,代表指定序列中滿足條件的項目數目。 |
Count<TSource>(IEnumerable<TSource>)
- 來源:
- Count.cs
- 來源:
- Count.cs
- 來源:
- Count.cs
- 來源:
- Count.cs
- 來源:
- Count.cs
傳回序列中的項目數目。
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>)
- 來源:
- Count.cs
- 來源:
- Count.cs
- 來源:
- Count.cs
- 來源:
- Count.cs
- 來源:
- Count.cs
傳回數位,代表指定序列中滿足條件的項目數目。
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的呼叫。