Stack.ToString() Method in C# with examples
ToString method is inherited from the Object class which is used to get a string that represents the current object. It can also apply on the Stack. It returns a string which represents the current stack object.
Syntax: public virtual string ToString ();
Return Value: This method returns a String representation of the collection.
Example 1: In the below program, GetType() method is used to get the type of the current object. It will clarify whether the given Stack object is converted into the string or not.
// C# program to demonstrate // Stack ToString() method using System; using System.Collections; class GFG { public static void Main(String[] args) { // Creating an Empty Stack Stack st = new Stack(); // Use Push() method // to add elements to // the stack st.Push("Welcome"); st.Push("To"); st.Push("Geeks"); st.Push("For"); st.Push("Geeks"); Console.WriteLine("The type of st before "+ "ToString Method: "+st.GetType()); Console.WriteLine("After ToString Method: "); foreach(string str in st) { // Using ToString() method Console.WriteLine(str.ToString()); } Console.WriteLine("The type of st after "+ "ToString Method: "+st.ToString().GetType()); } } |
The type of st before ToString Method: System.Collections.Stack After ToString Method: Geeks For Geeks To Welcome The type of st after ToString Method: System.String
Example 2:
// C# program to demonstrate // Stack ToString() method using System; using System.Collections; class GFG { public static void Main(String[] args) { // Creating an Empty Stack Stack st = new Stack(); // Use Push() method // to add elements to // the stack st.Push(1); st.Push(2); st.Push(3); st.Push(4); st.Push(5); Console.WriteLine("The type of st before "+ "ToString Method: "+st.GetType()); Console.WriteLine("After ToString Method: "); foreach(int i in st) { // Using ToString() method Console.WriteLine(i.ToString()); } Console.WriteLine("The type of st after "+ "ToString Method: "+st.ToString().GetType()); } } |
The type of st before ToString Method: System.Collections.Stack After ToString Method: 5 4 3 2 1 The type of st after ToString Method: System.String
Recommended Posts:
- MathF.Exp() Method in C# with Examples
- MathF.Pow() Method in C# with Examples
- MathF.Tan() Method in C# with Examples
- MathF.Cos() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- MathF.Abs() Method in C# with Examples
- MathF.Log() Method in C# with Examples
- MathF.Max() Method in C# with Examples
- MathF.Min() Method in C# with Examples
- Int16.GetTypeCode Method in C# with Examples
- Int64.GetTypeCode Method in C# with Examples
- Int16.GetHashCode Method in C# with Examples
- Int32.GetTypeCode Method in C# with Examples
- Int32.GetHashCode Method in C# with Examples
- BitArray.LeftShift() Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



