Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Stack.ToString() Method in C# with examples
The Stack.ToString() method in C# is used to get the string representation of the Stack class object. This method returns the fully qualified type name of the stack instance, not the contents of the stack. To display stack elements, you need to iterate through the collection and call ToString() on individual elements.
Syntax
Following is the syntax for the Stack.ToString() method −
public override string ToString();
Return Value
This method returns a string that represents the current Stack object. It returns the type name "System.Collections.Stack".
Using ToString() on Stack Elements
The following example demonstrates how to use ToString() on individual stack elements while iterating through the collection −
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack = new Stack();
stack.Push(150);
stack.Push(300);
stack.Push(500);
stack.Push(750);
stack.Push(1000);
stack.Push(1250);
stack.Push(1500);
stack.Push(2000);
stack.Push(2500);
Console.WriteLine("Stack elements...");
foreach(int val in stack) {
Console.WriteLine(val.ToString());
}
Console.WriteLine("Count of elements = " + stack.Count);
stack.Push(3000);
stack.Push(3500);
stack.Push(4000);
Console.WriteLine("\nStack elements...updated");
foreach(int val in stack) {
Console.WriteLine(val.ToString());
}
Console.WriteLine("\nCount of elements (updated) = " + stack.Count);
Console.WriteLine("\nStack object toString(): " + stack.ToString());
}
}
The output of the above code is −
Stack elements... 2500 2000 1500 1250 1000 750 500 300 150 Count of elements = 9 Stack elements...updated 4000 3500 3000 2500 2000 1500 1250 1000 750 500 300 150 Count of elements (updated) = 12 Stack object toString(): System.Collections.Stack
Using ToString() with String Elements
The following example shows using ToString() on string elements in a stack −
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack = new Stack();
stack.Push("Inspiron");
stack.Push("Alienware");
stack.Push("Projectors");
stack.Push("Monitors");
stack.Push("XPS");
stack.Push("Laptop");
stack.Push("Notebook");
Console.WriteLine("Stack elements...");
foreach(string val in stack) {
Console.WriteLine(val.ToString());
}
Console.WriteLine("Count of elements = " + stack.Count);
Console.WriteLine("Element at the top = " + stack.Peek());
stack.Push("Ultrabook");
stack.Push("Cameras");
stack.Push("Keyboards");
Console.WriteLine("\nStack elements...updated");
foreach(string val in stack) {
Console.WriteLine(val.ToString());
}
Console.WriteLine("Element at the top = " + stack.Peek());
Console.WriteLine("\nCount of elements (updated) = " + stack.Count);
Console.WriteLine("Stack type: " + stack.ToString());
stack.Clear();
Console.WriteLine("Count of elements (after clear) = " + stack.Count);
}
}
The output of the above code is −
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Element at the top = Notebook Stack elements...updated Keyboards Cameras Ultrabook Notebook Laptop XPS Monitors Projectors Alienware Inspiron Element at the top = Keyboards Count of elements (updated) = 10 Stack type: System.Collections.Stack Count of elements (after clear) = 0
Key Points
The
Stack.ToString()method returns the type name, not the stack contents.To display stack elements, iterate through the collection and call
ToString()on individual items.Stack follows LIFO (Last In, First Out) principle − the most recently added element is at the top.
Use
Peek()to view the top element without removing it from the stack.
Conclusion
The Stack.ToString() method returns the type information of the stack object rather than its contents. To display actual stack elements, you must iterate through the collection and call ToString() on each individual element, which is useful for debugging and displaying stack contents.
