📚Stacks in C# explained, how to use them and when to use them

Поділитися
Вставка
  • Опубліковано 6 жов 2024
  • Welcome, this is a C# tutorial about stacks
    🚀 C# Progress Academy - Become a job-ready C# and Angular web developer to land your dream developer role: bit.ly/45vbPUg
    Read the article and get the code: tutorials.eu/s...
    In this video you will learn about stacks in C#, how and when to use them and what is the difference with queues
    In this tutorial we will see how to implement a stack in C#
    Stacks are usually used when data reversal is needed or is a solution to our problems as we will see in the example at the end of
    Defining a stack in C#
    we can define a stack in the same way we define any collection
    here we will define a stack that takes integer numbers
    Adding and viewing data in a Stack
    We can add elements to our stack using the method Push()
    add elements to our stack
    as we run the application we will see that every time we use the Peek() method we will get the last value we Pushed into our stack
    Removing Elements from a Stack
    To remove elements from our stack we will use the method Pop()
    which will remove the element on top of the stack and will return it at the same time
    of course we don't want to use Pop() on an empty stack since this will throw and error so first we need to check the Count property of the stack which will return how many elements are in our stack
    Removing Elements from a Stack
    To remove elements from our stack we will use the method Pop()
    which will remove the element on top of the stack and will return it at the same time
    of course we don't want to use Pop() on an empty stack since this will throw and error so first we need to check the Count property of the stack which will return how many elements are in our stack
    Example Reversing an array
    one of the many applications of stacks is data reversal in the following example we will use a stack to reverse an array
    The solution is simple
    1. we will go through the elements of the array from start to end and we will push each element into a stack
    2.After that we will pop and print every element in the stack until it's empty
    stack, push c#, c# stack size, c# stack to list, c# stack pop, c# stack peek, c# stack vs list
    tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
    This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
    Stay tuned and subscribe to tutorialsEU: goo.gl/rBFh3x

КОМЕНТАРІ • 8