Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Stack to array in C#
To convert stack to the array, the code is as follows −
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Stack<string> stack = new Stack<string>();
stack.Push("AB");
stack.Push("CD");
stack.Push("FG");
stack.Push("KL");
Console.WriteLine("Array...");
foreach(string i in stack){
Console.WriteLine(i);
}
string[] strArr = stack.ToArray();
Console.WriteLine("Convert Stack to Array...");
foreach(string i in strArr){
Console.WriteLine(i);
}
}
}
Output
This will produce the following output −
Array... KL FG CD AB Convert Stack to Array... KL FG CD AB
Example
Let us now see another example −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Stack<int> stack = new Stack<int>();
stack.Push(250);
stack.Push(500);
stack.Push(750);
stack.Push(1000);
stack.Push(1200);
stack.Push(1500);
Console.WriteLine("Array...");
foreach(int i in stack){
Console.WriteLine(i);
}
int[] intArr = stack.ToArray();
Console.WriteLine("Convert Stack to Array...");
foreach(int i in intArr){
Console.WriteLine(i);
}
}
}
Output
This will produce the following output −
Array... 1500 1200 1000 750 500 250 Convert Stack to Array... 1500 1200 1000 750 500 250
Advertisements