C# language: 14. Collections and lists

Today about the collections, there are such concepts as a heap or stack, so if you were dealing with an assembler or some other low-level language, then you shouldn’t have no problem understanding this lesson.


You had already dealt with the simplest collection, it is a array, i.e. I am just stuffing a few variables there, but if for example we would like to sort or reverse the order, other functions would have to be created and could be a problem and there is something in C# like lists, they differ from the tables that you can do with the content of the list whatever you want, you can reverse the order, add another value at any time, sort etc. without creating any functions because the lists already have ready functions. Let’s add to the list of 5 numbers in the Main function:

And let’s try some of its functions. To start with, let’s display the number of its elements:

And again, we add one number to the list and display it on the screen:

Then we check if the list has a number of 10:

And let’s check where the list contains the number 10:

We remove two elements from the list and check how many numbers it has now:

Let’s display the contents of the array:

Let’s reverse the contents of the list and display it on the screen:

Let us now create collections that are stack, those who have already dealt with low level languages certainly know, and those who do not know it, the stack works in such a way that you can remove the variable at the end and the top of stack is the last postponed element in contrast to the list in which the last element landed at the end. Let’s do something like this:

And what did we do here? First, we determined what to put on the stack, then using the foreach loop and the Push() function we put all the numbers off the array and finally remove all the values from the stack using the Count property and the Pop() method

This content also you can find on my steemit blog: https://steemit.com/programming/@slawas/c-language-14-collections-and-lists

And on medium: https://medium.com/@sawomirkowalski/c-language-14-collections-and-lists-1e640073e8f1

That’s all, in the next lesson will be the so-called delegates, see you!

If that post was useful for you share it with your friends :)

Post a comment

Notify about