Lists

Introduction

List is a collection similar to array but with dynamic size.

Simply it's size is not necessary to be set and changes when element is added / removed.

In order to use it the following line should be added:

Syntax

type specifies the values type - int, double, string etc...

Initialization

Empty

With Elements

Adding element to the back

This can be done via the Add function. Like in the following code:

Accessing Element

Elements are accessed via their index.

Let's add elements to list and access the element at index 1.

Output:

Outside of bounds

Accessing element outside of list's bounds results in exception.

Let's look at the following example

numbers has 3 elements (indexes from 0 to 2) but at line 16 the 3rd index is accessed.

Size

Size is the count of elements in collection.

In order to get it the Count property is used.

Example:

Output:

Looping elements

Let's have a list of ages and print only those who are greater than 20.

Output:

It can be done via for-loop too.