Home » C# programs » C# Data structure programs

C# program to convert queue into object array

Here, we are implementing a C# program that will convert queue into an object array. Here we are using ToArray() method of Queue class.
Submitted by IncludeHelp, on December 29, 2017

Given a queue with some elements and we have to convert queue to an object array using C# program.

object[] ToArray()

Syntax:

object[] ToArray(); 

Return value:

This method returns object array.

Program to convert queue into object array in C#

using System;
using System.Collections;

namespace ConsoleApplication1
{
    
    class Program
    {
        static void Main()
        {
            object[] arr = new object[5];
            Queue Q = new Queue(5);

            Q.Enqueue(10);
            Q.Enqueue(20);
            Q.Enqueue(30);
            Q.Enqueue(40);

            arr = Q.ToArray();

            Console.WriteLine("Items are:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("\tItem[" + (i + 1) + "]: " + Convert.ToInt32(arr[i]));
            }
        }
    }
}

Output

Items are:
        Item[1]: 10
        Item[2]: 20
        Item[3]: 30
        Item[4]: 40

In this program, we declared an object of Queue class and then inserted elements into queue. Here we convert Q into object array arr.

Note: In above program, to use 'Queue' class, we need to include System.Collection namespace.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.