System.Collections.Queue.CopyTo C# (CSharp) Method

CopyTo() public method

public CopyTo ( Array array, int index ) : void
array Array
index int
return void
        public virtual void CopyTo(Array array, int index)
        {
            if (array == null)
                throw new ArgumentNullException(nameof(array));
            if (array.Rank != 1)
                throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array));
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
            Contract.EndContractBlock();
            int arrayLen = array.Length;
            if (arrayLen - index < _size)
                throw new ArgumentException(SR.Argument_InvalidOffLen);

            int numToCopy = _size;
            if (numToCopy == 0)
                return;
            int firstPart = (_array.Length - _head < numToCopy) ? _array.Length - _head : numToCopy;
            Array.Copy(_array, _head, array, index, firstPart);
            numToCopy -= firstPart;
            if (numToCopy > 0)
                Array.Copy(_array, 0, array, index + _array.Length - _head, numToCopy);
        }

Same methods

Queue::CopyTo ( System array, int index ) : void

Usage Example

Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //New Queue of Integers

            Queue<int> queue = new Queue<int>();
            queue.Enqueue(5);
            queue.Enqueue(10);
            queue.Enqueue(15);
            queue.Enqueue(20);

            /*Create new array with
            length equal to Queue's element count*/

            int[] array = new int[queue.Count];

            //Copy the Queue to the Array
            queue.CopyTo(array, 0);

            //Loop through and display int[] in order
            Console.WriteLine("Array: ");

            for (int i=0; i < array.Length; i++)
            {
                Console.WriteLine(array[i]);
            }

            //Loop through int array in reverse order

            Console.WriteLine("Array reverse order: ");
            for (int i = array.Length -1; i >= 0; i--)
            {
                Console.WriteLine(array[i]);

            }
        }
All Usage Examples Of System.Collections.Queue::CopyTo