C# language: 14. Collections and lists

in #programming7 years ago

CC.png

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.

Collections

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:

List<int> list = new List<int>();
int number = 0;
 
for(int i=0; i<5; i++)
{
    number += 5;
    list.Add(number);
}

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

int Size = list.Count;
Console.WriteLine(Size);

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

int add = 45;
list.Add(add);
int addMore = list.Count;
Console.WriteLine(addMore);

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

bool ifNumber = list.Contains(10);
Console.WriteLine(ifNumber);

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

int index = list.IndexOf(10);
Console.WriteLine(index);

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

Console.WriteLine(list.Remove(5));
Console.WriteLine(list.Remove(15));
int check = list.Count;
Console.WriteLine(check);

Let’s display the contents of the array:

foreach(int a in list)
{
     Console.WriteLine(a);
}

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

list.Reverse();
foreach (int b in list)
{
     Console.WriteLine(b);
}

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:

int[] array = {12,34,23,78,23 };
Stack<int> stack = new Stack<int>();
 
foreach (int x in array)
{
    stack.Push(x);
}
 
while (stack.Count > 0)
{
    Console.WriteLine(stack.Pop());
}

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 blog http://devman.pl/csharplan/c-language-14-collections-lists/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

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

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63126.26
ETH 2596.37
USDT 1.00
SBD 2.76