Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# stack

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoApplication
{
 class Program
 {
  static void Main(string[] args)
  {
   Stack st = new Stack();
   st.Push(1);
   st.Push(2);
   st.Push(3);
   
   st.Pop();

   foreach (Object obj in st)
   {
    Console.WriteLine(obj);
   }
    Console.ReadKey();
  }
 }
}
Comment

c# stack

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoApplication
{
 class Program
 {
  static void Main(string[] args)
  {
   Stack st = new Stack();
   st.Push(1);
   st.Push(2);
   st.Push(3);

   foreach (Object obj in st)
   {
    Console.WriteLine(obj);
   }
    Console.WriteLine(); Console.WriteLine();
    Console.WriteLine("The number of elements in the stack " +st.Count);
    Console.WriteLine("Does the stack contain the elements 3 "+st.Contains(3));
    Console.ReadKey();
  }
 }
}
Comment

c# stack

Stack.push(element)
Comment

c# stack

Stack st = new Stack()
Comment

c# stack

Stack.pop()
Comment

c# stack

Stack.Count
Comment

c# stack

Stack.Contains(element)
Comment

stack in c#

/* Stack
 * Stack follows the LIFO last in First Out
 * It does not have a fixed size and increase according to the elements that have added to it.
 * We have stavk in both Syste.Collections nd System.Collections.Generic
 * But we recommended to use the generic Stack<T>
 * no boxing and unboxing fro generic stack but for the non generic stack you have to us the unvoxing methods 
 * Stack element can be added or deleted
 * heneric stack have specific data types elemnts
 */
//Create a stack
using System.Collections.Generic;
//Declaring a stack
Stack<int> s1 = new Stack<int>();
//Declare using var keyword
var s2  = new Stack<string>();
//Declare and initilze a stack simultaneousley
//You cant do that


//Adding elements to the stack :  Push() : Insert the item at the top of the stack
s1.Push(10);
s1.Push(20);
s1.Push(30);
//Here the stacj will be like this 30, 20, 10 : but the 30 added in the last 
s2.Push("Subhasis");
s2.Push("Aditi");

//Create a stack from an array
int[] a = new int[] { 4, 5, 6 };
Stack<int> s3 = new Stack<int>(a);

//Looping through stack
foreach(var item in s3)
    Console.WriteLine(item);
//Deleting from the stack :  Pop()
//The last element which is added to the  stack will be deleted when we use the pop method
s3.Pop();
foreach (var i in s3)
    Console.WriteLine($"The values after pop methods are : {i}");
/*The values after pop methods are : 5
The values after pop methods are : 4*/
s1.Pop();
foreach (var g in s1)
    Console.WriteLine($"The values in s1 after pop() : {g}");
/*The values in s1 after pop() : 20
The values in s1 after pop() : 10*/

//Take a peek at te last element : Peek() : Do not delete the last element just look at that value

Console.WriteLine(s1.Peek());
Console.WriteLine(s2.Peek());
Console.WriteLine(s3.Peek());
/*20
Aditi
5*/

//Count : give the numvber of elements in the stack
Console.WriteLine(s1.Count);
//Contains : check if the element is there or not
Console.WriteLine(s2.Contains("Aditi"));
/*2
True*/


Comment

PREVIOUS NEXT
Code Example
Csharp :: c# .net set exception data 
Csharp :: wpf user parent controller datacontext 
Csharp :: how to change an object color with fill c# 
Csharp :: Enum into table C# 
Csharp :: pun 2 matchmaking custom room properties 
Csharp :: textbox center align winform 
Csharp :: cmd command see which groups a user is in 
Csharp :: temp^late php table for mysql 
Csharp :: asp.net core user.identity.name is null 
Csharp :: unity oculus vibrate 
Csharp :: c sharp if statements 
Csharp :: virtual properties and lazy loading in c# 
Csharp :: remotefx 3d video adapter warning 
Csharp :: c# summary angle brackets 
Csharp :: gridview column cell alignment form c# 
Csharp :: c# switch expression 8.0 
Csharp :: Set database timeout in Entity Framework 
Csharp :: xamarin forms set the grid row property of an element programmatically 
Csharp :: make wpf run in fullscreen but above windows taskbar 
Csharp :: membership get user id 
Csharp :: unity save slots 
Csharp :: serenity.is required field 
Csharp :: how to make a draggable visual studio panel 
Csharp :: enum extends dictionary c# 
Csharp :: aquarette 
Csharp :: AutoFixture ignore property 
Csharp :: Conditional IQueryable Linq extension 
Csharp :: entity framework where date between 
Csharp :: Difference between Math.Floor() and Math.Truncate() 
Csharp :: how to get the size of an array in c# 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =