System.Collections.Stack CopyTo() Method
How to copy the Stack to an existing one-dimensional Array, starting at the specified array index
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class Program { static void Main(string[] args) { Stack colors = new Stack(); colors.Push("Red"); colors.Push("DarkRed"); colors.Push("IndianRed"); colors.Push("MediumVioletRed"); Console.WriteLine("Stack Elements... "); foreach (string color in colors) { Console.WriteLine(color); } string[] redColors = new string[colors.Count]; colors.CopyTo(redColors, 0); Console.WriteLine("\nAfter Call CopyTo(array, index 0) Method, Array Elements..."); for (int i = 0; i < redColors.Length; i++) { Console.WriteLine(redColors[i].ToString()); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Stack Elements... MediumVioletRed IndianRed DarkRed Red After Call CopyTo(array, index 0) Method, Array Elements... MediumVioletRed IndianRed DarkRed Red |