c sharp

C# Stack

If you have ever worked on Collection in programming, then you may have heard about the “stack” collection in object-oriented programming. Stack is a collection of objects that works on the rule of first-come, last serve. The stack collection is utilized where we require to fetch the data entered at the last before every other item. C# came up with the “Stack” collection class in its “System.Collections” namespace to add or remove data items. We will be discussing how a stack can be used in the C# example codes while using the Ubuntu 20.04 platform.

Example # 01: Push and Pop Function

We will be starting our first example of using Stack in C# with its push() and pop() functions. The “push()” function in C# is especially used to insert values within the stack collection while the “pop()” function is utilized to remove the last inserted item from the stack. So, we have been starting our first example with the use of the “System” library and the “collections” namespace in the first two lines.

We have created a class “Test” which is using a Main() driver function to execute this program. We have initialized an object “s” for Stack collection at the first line of the main() function. To insert values or items in the stack, we have been using the ‘Push’ function of C# with the help of the stack object “s”. We have added 5 string values first and then a numerical string value at the last index of the stack.

A total of 6 values have been added. Now, to iterate the values of the stack just like arrays, we have to use the “foreach” loop utilizing the object “s” of the stack. It will continue to display the values of the stack until the loop ends. The most important thing to note here is that, it will display the last-in value first. Now, this code is ready for use. So, we have saved it with Ctrl+S first.

After saving the code, we have to try the “mcs” compiler followed by the name of a C# file to compile our code. It has created an “exe” file for our execution. So, we have tried this “exe” file within the “mono” instruction to display the output. The output stack displayed on the shell is showing the last-in value “14” first, while the first-added value “Hello” at the last.

After looking at the push() function for stacks, we will be taking a look at the pop() function to remove the last-added items from the stack in C#. So, we have been utilizing the “System” library and “System.Collections” class of a C# in the code. The Test class Main() function is initialized with a stack collection object “s”. This object “s” is used for calling the “Push” function to insert two string values at the first two locations of a stack.

Here comes the main() part. We have called the “pop” function with the stack object “s” to remove the last-added value from the stack i.e. “World”. After this, we have used the Push() function again to add two more values and used the pop() function again to remove the last-added value from the stack. Lastly, we have added three more values to the stack using the “push” function. Using the “foreach” loop, we are displaying all the values of a stack in the last-added, first-serve manner.

After saving this code, we have compiled and executed its “cs” and “exe” files. The output returned by the code shows us the last value added by a push function displayed first on the screen. Also, the values added before the use of the “pop” function are removed from the stack and not displayed i.e., “World”, and “Windows”.

Example # 02: Count and Peek Function

We can count the values of a stack with the Count() function of C# and take a look at the last-added value of the stack using the “Peek” function of C# for stacks. Within the Main() function, we have initialized a stack object “s” and used this object to call the “push()” function to insert 3 string values in it. After this, we have utilized the WriteLine() statements to call the Count() and Peek() functions using the object “s” to display the total number of elements in the stack and the last-added value.

After executing this C# codes “exe” file, we have got to know that the stack has a total of 3 values and the last-added value is “Ubuntu”.

Example # 03: Contains Function

The stack’s “Contains” function is specifically designed to check if the specified value is residing in the stack or not regardless of its location. So, we have initialized a stack object “s” and added some values to it using the Push() function. Along with that, we have also used the pop() function to remove some of the just-added stack values.

After adding the values in the stack, we have utilized 2 Console.WriteLine() function statements. Both statements are utilizing the object “s” calling the Contains() function from the Stack class of Collections namespace to search for the specified values. We have specified “Hello” and “14” in the parameters of the Contains() function in the Console.WriteLine() statement. This will return a boolean “true” or “false” according to the circumstances. Let’s save our code and see its result.

We have compiled the “cs” file for this code and executed its newly generated “exe” file on our shell with the use of the “mcs” and “mono” commands. The result displayed on the screen is showing “True” for both the specified values in the Contains function. This means both the values “Hello” and “14” are available in the stack.

Example # 04:

We will be discussing the way of converting a stack into an array with the help of a “ToArray” function of the Collections class. So, after initializing a stack “s”, we have added a total of 5 elements in it with the use of the “push” function. After this, we have created an object type array “Arr” calling the “ToArray()” function with the help of the “s” object to convert the stack “s” to an array.

It will add the last value of the stack at the first index of an array. The “foreach” loop is here to iterate the values of array Object “Arr” and display the one by one using the Console.WriteLine() function statement. Let’s save this code before the execution.

After compiling this code, we executed its “exe” file and got the values of an array generated by a stack displayed in a first-come-first-serve manner.


Within this guide today, we have seen how a collection named “Stack” can be utilized in C# when you don’t have any knowledge about arrays. We have discussed the use of different functions of the stack to insert, delete, count, check, and display the values of a stack using the Push(), Pop(), Count(), Contains(), and Peek() functions respectively. Along with that, we have utilized the “ToArray” function of stacks to convert a stack to an array in C# and display the array values in the opposite manner of stacks.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.