📜  C#中的Queue.Count属性

📅  最后修改于: 2021-05-29 21:18:53             🧑  作者: Mango

此属性用于获取队列中包含的元素数。检索此属性的值是一个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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.queue.count?view=netframework-4.7.2