Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Proprietà Queue<T>.Count

 

Data di pubblicazione: ottobre 2016

Ottiene il numero di elementi contenuti in Queue<T>.

Spazio dei nomi:   System.Collections.Generic
Assembly:  System (in System.dll)

public int Count { get; }

Valore proprietà

Type: System.Int32

Il numero di elementi contenuti in Queue<T>.

The capacity of a T:System.Collections.Generic.Queue`1 is the number of elements that the T:System.Collections.Generic.Queue`1 can store. P:System.Collections.Generic.Queue`1.Count is the number of elements that are actually in the T:System.Collections.Generic.Queue`1.

The capacity is always greater than or equal to P:System.Collections.Generic.Queue`1.Count. If P:System.Collections.Generic.Queue`1.Count exceeds the capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.

Retrieving the value of this property is an O(1) operation.

The following code example demonstrates several properties and methods of the T:System.Collections.Generic.Queue`1 generic class, including the P:System.Collections.Generic.Queue`1.Count property.

The code example creates a queue of strings with default capacity and uses the M:System.Collections.Generic.Queue`1.Enqueue(`0) method to queue five strings. The elements of the queue are enumerated, which does not change the state of the queue. The M:System.Collections.Generic.Queue`1.Dequeue method is used to dequeue the first string. The M:System.Collections.Generic.Queue`1.Peek method is used to look at the next item in the queue, and then the M:System.Collections.Generic.Queue`1.Dequeue method is used to dequeue it.

The M:System.Collections.Generic.Queue`1.ToArray method is used to create an array and copy the queue elements to it, then the array is passed to the M:System.Collections.Generic.Queue`1.#ctor(System.Collections.Generic.IEnumerable{`0}) constructor that takes T:System.Collections.Generic.IEnumerable`1, creating a copy of the queue. The elements of the copy are displayed.

An array twice the size of the queue is created, and the M:System.Collections.Generic.Queue`1.CopyTo(`0[],System.Int32) method is used to copy the array elements beginning at the middle of the array. The M:System.Collections.Generic.Queue`1.#ctor(System.Collections.Generic.IEnumerable{`0}) constructor is used again to create a second copy of the queue containing three null elements at the beginning.

The M:System.Collections.Generic.Queue`1.Contains(`0) method is used to show that the string "four" is in the first copy of the queue, after which the M:System.Collections.Generic.Queue`1.Clear method clears the copy and the P:System.Collections.Generic.Queue`1.Count property shows that the queue is empty.

using System;
using System.Collections.Generic;

class Example
{
    public static void Main()
    {
        Queue<string> numbers = new Queue<string>();
        numbers.Enqueue("one");
        numbers.Enqueue("two");
        numbers.Enqueue("three");
        numbers.Enqueue("four");
        numbers.Enqueue("five");

        // A queue can be enumerated without disturbing its contents.
        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
        Console.WriteLine("Peek at next item to dequeue: {0}", 
            numbers.Peek());
        Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());

        // Create a copy of the queue, using the ToArray method and the
        // constructor that accepts an IEnumerable<T>.
        Queue<string> queueCopy = new Queue<string>(numbers.ToArray());

        Console.WriteLine("\nContents of the first copy:");
        foreach( string number in queueCopy )
        {
            Console.WriteLine(number);
        }

        // Create an array twice the size of the queue and copy the
        // elements of the queue, starting at the middle of the 
        // array. 
        string[] array2 = new string[numbers.Count * 2];
        numbers.CopyTo(array2, numbers.Count);

        // Create a second queue, using the constructor that accepts an
        // IEnumerable(Of T).
        Queue<string> queueCopy2 = new Queue<string>(array2);

        Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
        foreach( string number in queueCopy2 )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}", 
            queueCopy.Contains("four"));

        Console.WriteLine("\nqueueCopy.Clear()");
        queueCopy.Clear();
        Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
    }
}

/* This code example produces the following output:

one
two
three
four
five

Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'

Contents of the copy:
three
four
five

Contents of the second copy, with duplicates and nulls:



three
four
five

queueCopy.Contains("four") = True

queueCopy.Clear()

queueCopy.Count = 0
 */

Universal Windows Platform
Disponibile da 8
.NET Framework
Disponibile da 2.0
Libreria di classi portabile
Supportato in: piattaforme .NET portabili
Silverlight
Disponibile da 2.0
Windows Phone Silverlight
Disponibile da 7.0
Windows Phone
Disponibile da 8.1
Torna all'inizio
Mostra: