C# Basic – Lesson 4

C# Basic – Lesson 4: Arrays and Lists

Hi and welcome to the Lesson 4 – Arrays and Lists on our tutorial series Learning C# with Zenva.

Tutorial Source Code

All of the source code for this tutorial can be downloaded here.

In this lesson, we will see what is an array and what is a list; the difference between them; how to declare and use them together with the for statement we learnt in the last lesson.


An Array is a data collection of the same type. A feature of the arrays is that they are static, that is, the programmer cannot increase the amount of positions in runtime. So if we declare an array of int with 10 positions, these positions will remain until the end of the program for this array.

Arrays have the following syntax:

Data Type: specify the type of elements in the array.

[ ] : specify the size of the array.

arrayName: specify the name of the array.

When we declare an array, it does not mean it is initialized in the memory because by now, we cannot assign values to it.

Arrays, lists and objects, need to be initialized so we can assign them values. To initialized these structs, we use the word new. For example:

Remember: An array start its index in position 0. It means, an array with 3 positions will have as index: [0][1][2]. We can have access to the array content using the index. There are several ways to assign value to an array. Let’s see them:

Using the index number:

When we declare the array:

We can also omit the size:

Note: ‘//’ is used to write comments in the code.

We can assign a value from an array, using the index, to a variable:

Time to write some code! Let’s start a new Project and name it ArrayAndList. Write or copy and paste the code below.

Array example:

In this example, we declared an array of type string with 3 positions and we wrote one name on each position. Then we showed that content of the array using a flow control we learnt last lesson, “for”, to go through each position of the array. Reading the command: “from i = 0 to names.length (in this case length = 3), do something, then, increment the variable i.”

Running our application:


We can also use the structure for to fill our array with numbers, for instance:


A List(T) has the same characteristics of an array. However, a List(T) can be increased dynamically. In a List(T), we do not establish its length. As we need more values to be put into a list, we simply write the method ‘Add’ and we add a new value.

A List(T) has the following syntax:

Also to initialize the list we use the word ‘new’.

Both arrays and lists are very easy to work. Also, they are simple and powerful.

List example:

In this example, we did the multiplication table 9. It was declared a list of type int and we add some numbers using the repeat loop for. Then we showed its content by using the foreach. Also, we need to declare on the top of the code, the using System.Collections.Generic to be able to use List(T).

Running our application:


We can also add items provided by an array in a list. With an array to check its size, we use the method length. With a List(T), we use the method Count.

Count: We can assign the size of a list to a variable by writing:

Clear: To remove all the elements from a list we use the function Clear().

IndexOf: Give us the element index of a value in the List(T). When the method IndexOf does not find the value, it returns as value -1;

Reverse: Invert the order of the elements in the list.

Distinct: Remove duplicates elements in the list. This method belongs to System.Linq, so we need to add this using System.Linq in our application.

This is the end of this tutorial. I hope you have enjoyed and I see you in the next lesson!

Thank you!

Published by

Allan Carlos Claudino Villa

Allan Villa is Bachelor in Information Systems and has been software developer for 4 years. During his studies, he was granted with a scholarship and he studied in Australia at The University Of Queensland for 1 year, where he learnt more and more about software. Recently, Villa is working as a software developer at a Health Software Company in Brazil.

Share this article