Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What are Lists? | Additional Structures & File Handling
course content

Зміст курсу

C# Beyond Basics

What are Lists?What are Lists?

Imagine we have a situation where we want to store the names of all the students that are taking a certain course. The first solution we can come up is to create an Array:

cs

index.cs

However, if at some point are more than 50 students, we won't be able to store their names. Similarly, in a situation where there are less than 50 students, the unoccupied spaces in the array will be a waste of memory - this becomes a problem in especially large arrays.

Here we need a new structure which can store a variable amount of elements. Luckily such a structure already exists, it's called a List.

Lists are very similar to Arrays, however the number of elements that are stored in a list are changeable.

It is possible to modify existing elements in an Array however we cannot add new entires to it.

Following is the syntax for declaring an empty list:

Using this syntax we can create a list for storing the Students' names:

cs

index.cs

It is important to note that we need to import the Generic module to be able to use Lists.

You simply need to add this line under using System; to import the required module.

Add() Method

We can add elements to a list by using the Add method:

cs

index.cs

The above code will add four elements to the students list.

Just like an Array of type string can only contain string elements. A list of type string can only accept string elements.

Indexing

The first element is Anna hence it will have the index 0, while Laura will have the index 1 and so on. Elements of a list can be accessed via indexing just like an array:

cs

index.cs

Count() Method

We can retrieve the length of a list using its Count attribute:

cs

index.cs

Dynamic Length

Note that the length of a list is dynamic (changeable), so it changes as we add elements:

cs

index.cs

Initialization

We can also initialize a list with some elements using the following syntax:

For-example:

cs

index.cs

Looping Through Lists

Looping through a List similar to how we would loop through Arrays

- Using a for-loop:

cs

index.cs

- Using a foreach loop

cs

index.cs

Tip:

To make the declaration syntax shorter we can also use implicit declaration. To recall, an explicit declaration is when we specify the data type during variable declaration, for-example:

cs

index.cs

On the other hand, in implicit declaration we can simply use the var keyword and the compiler automatically infers the data type of the variable according to the assigned value:

cs

index.cs

We can use implicit declaration when declaring lists as well:

For-example:

cs

index.cs

1. What will be the output of the following code:
2. Which module is needed to be imported for using lists?
3. Which method is used for retrieving the size (length) of a list?

What will be the output of the following code:

Виберіть правильну відповідь

Which module is needed to be imported for using lists?

Виберіть правильну відповідь

Which method is used for retrieving the size (length) of a list?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 1
course content

Зміст курсу

C# Beyond Basics

What are Lists?What are Lists?

Imagine we have a situation where we want to store the names of all the students that are taking a certain course. The first solution we can come up is to create an Array:

cs

index.cs

However, if at some point are more than 50 students, we won't be able to store their names. Similarly, in a situation where there are less than 50 students, the unoccupied spaces in the array will be a waste of memory - this becomes a problem in especially large arrays.

Here we need a new structure which can store a variable amount of elements. Luckily such a structure already exists, it's called a List.

Lists are very similar to Arrays, however the number of elements that are stored in a list are changeable.

It is possible to modify existing elements in an Array however we cannot add new entires to it.

Following is the syntax for declaring an empty list:

Using this syntax we can create a list for storing the Students' names:

cs

index.cs

It is important to note that we need to import the Generic module to be able to use Lists.

You simply need to add this line under using System; to import the required module.

Add() Method

We can add elements to a list by using the Add method:

cs

index.cs

The above code will add four elements to the students list.

Just like an Array of type string can only contain string elements. A list of type string can only accept string elements.

Indexing

The first element is Anna hence it will have the index 0, while Laura will have the index 1 and so on. Elements of a list can be accessed via indexing just like an array:

cs

index.cs

Count() Method

We can retrieve the length of a list using its Count attribute:

cs

index.cs

Dynamic Length

Note that the length of a list is dynamic (changeable), so it changes as we add elements:

cs

index.cs

Initialization

We can also initialize a list with some elements using the following syntax:

For-example:

cs

index.cs

Looping Through Lists

Looping through a List similar to how we would loop through Arrays

- Using a for-loop:

cs

index.cs

- Using a foreach loop

cs

index.cs

Tip:

To make the declaration syntax shorter we can also use implicit declaration. To recall, an explicit declaration is when we specify the data type during variable declaration, for-example:

cs

index.cs

On the other hand, in implicit declaration we can simply use the var keyword and the compiler automatically infers the data type of the variable according to the assigned value:

cs

index.cs

We can use implicit declaration when declaring lists as well:

For-example:

cs

index.cs

1. What will be the output of the following code:
2. Which module is needed to be imported for using lists?
3. Which method is used for retrieving the size (length) of a list?

What will be the output of the following code:

Виберіть правильну відповідь

Which module is needed to be imported for using lists?

Виберіть правильну відповідь

Which method is used for retrieving the size (length) of a list?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 1
some-alt