Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Nov 3, 2022, 1:05 PM EDT
https://www.c-sharpcorner.com/UploadFile/c25b6d/working-on-stack-using-C-Sharp/
Satya KarkiRank #78 · 24.6K reputationPosted Nov 3, 2022, 9:56 PM EDT
Hi,
Represents a simple last-in-first-out (LIFO) non-generic collection of objects.
Example:
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack sampleStack = new Stack(); sampleStack.Push("Hi"); sampleStack.Push("rijsat.com"); sampleStack.Push("!"); // Displays the properties and values of the Stack. Console.WriteLine( "sampleStack" ); Console.WriteLine( "\tCount: {0}", sampleStack.Count ); Console.Write( "\tValues:" ); PrintValues( sampleStack); } public static void PrintValues( IEnumerable myCollection ) { foreach ( Object obj in myCollection ) Console.Write( " {0}", obj ); Console.WriteLine(); } } /* This code produces the following output. sampleStack Count: 3 Values: ! rijsat.com Hi */https://learn.microsoft.com/en-us/dotnet/api/system.collections.stack?view=net-6.0
Rijwan AnsariRank #82 · 24.2K reputationPosted Nov 3, 2022, 10:01 PM EDT
Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. This class comes under System.Collections namespace.
// C# code to create a Stack using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Stack Stack myStack = new Stack(); // Inserting the elements into the Stack myStack.Push("1st Element"); myStack.Push("2nd Element"); myStack.Push("3rd Element"); myStack.Push("4th Element"); myStack.Push("5th Element"); myStack.Push("6th Element"); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements in the Stack are : "); Console.WriteLine(myStack.Count); // Displaying the top element of Stack // without removing it from the Stack Console.WriteLine("Element at the top is : " + myStack.Peek()); // Displaying the top element of Stack // without removing it from the Stack Console.WriteLine("Element at the top is : " + myStack.Peek()); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements in the Stack are : "); Console.WriteLine(myStack.Count); } }https://www.geeksforgeeks.org/c-sharp-stack-class/
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Nov 6, 2022, 2:05 AM EST
https://www.tutorialsteacher.com/csharp/csharp-stack
Rajeev KumarRank #1,334 · 1.3K reputationPosted Mar 14, 2023, 3:48 AM EDT
Stack myStack = new Stack(); myStack.Push(100); myStack.Push(200); myStack.Push(300); myStack.Push(400); foreach (var item in myStack) Console.Write(item + ","); //Output are- 400,300,200,100