此屬性用於獲取隊列中包含的元素數。檢索此屬性的值是一個O(1)操作,它位於System.Collections命名空間下。
用法:
public virtual int Count { get; }
屬性值:此屬性返回隊列中包含的元素數。
以下示例程序旨在說明上述屬性的用法:
範例1:
// C# code to illustrate the
// Queue.Count Property
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Queue
Queue myQueue = new Queue();
// Displaying the count of elements
// contained in the Queue
Console.Write("Total number of elements"+
" in the Queue are:");
// The function should return 0
// as the Queue is empty and it
// doesn't contain any element
Console.WriteLine(myQueue.Count);
}
}
輸出:
Total number of elements in the Queue are:0
範例2:
// C# code to illustrate the
// Queue.Count Property
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Queue
Queue myQueue = new Queue();
// Inserting the elements into the Queue
myQueue.Enqueue("C");
myQueue.Enqueue("C++");
myQueue.Enqueue("Java");
myQueue.Enqueue("C#");
myQueue.Enqueue("HTML");
myQueue.Enqueue("CSS");
// Displaying the count of elements
// contained in the Queue
Console.Write("Total number of elements "+
"in the Queue are:");
Console.WriteLine(myQueue.Count);
}
}
輸出:
Total number of elements in the Queue are:6
參考:
相關用法
- C# Stack.IsSynchronized用法及代碼示例
- C# Stack.Count用法及代碼示例
- C# Queue.IsSynchronized用法及代碼示例
- C# Dictionary.Item[]用法及代碼示例
- C# Dictionary.Values用法及代碼示例
- C# Dictionary.Keys用法及代碼示例
- C# Dictionary.Count用法及代碼示例
- C# SortedDictionary.Count用法及代碼示例
- C# SortedDictionary.Keys用法及代碼示例
- C# SortedDictionary.Values用法及代碼示例
- C# SortedDictionary.Item[]用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Queue.Count Property in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。