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

Зміст курсу

C# Beyond Basics

List MethodsList Methods

In the previous chapter we learnt how to declare, initialize and add elements to a list. Additionally we also looked at how we would access elements of a list and loop through them. In this chapter we will look at some useful list methods.

1. Remove()

The Remove method removes the first instance of an element from a list.

Syntax: exampleList.remove(targetElement);

cs

index.cs

In case no such element is found, it simply doesn't do anything:

cs

index.cs

2. RemoteAt()

The RemoveAt method removes an element at a specific index.

Syntax: exampleList.RemoveAt(index);

cs

index.cs

3. Clear

The Clear method simply removes all elements from a list. It takes no arguments.

Syntax: exampleList.Clear();

cs

index.cs

4. Insert

The Insert method inserts an element in the list at a specified index.

Syntax: exampleList.Insert(index, elementToInsert);

cs

index.cs

We can omit the curly braces {} in case there's only a single statement inside an if-condition, a for-loop, or a foreach loop. For-example:

cs

index.cs

The above snippet is the same as:

cs

index.cs

If there's already an element at the specified index, it is pushed to the right, and so are the remaining elements of the array after it - in case there are any. The following diagram illustrates the process:

5. Contains()

The Contains method checks if a list contains a specific element. It returns a boolean value (true or false).

Syntax: exampleList.Contains(element);

cs

index.cs

6. IndexOf()

The IndexOf method returns the index of the first occurrence of an element in a list.

Syntax: exampleList.IndexOf(element);

cs

index.cs

If the element doesn't exist in the list, it simply returns -1:

The indexOf method is especially useful when we want to access an element by index but we won't know it's index. For-example in the vowels, we want to access the element o by index and change it to capital O:

cs

index.cs

1. What does the `Remove()` method do?
2. What is the fastest way of checking if a list contains a specific element?
3. Which method is used for removing all elements from a list?
4. What will be the output of the following code?

What does the Remove() method do?

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

What is the fastest way of checking if a list contains a specific element?

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

Which method is used for removing all elements from a list?

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

What will be the output of the following code?

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

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

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

Зміст курсу

C# Beyond Basics

List MethodsList Methods

In the previous chapter we learnt how to declare, initialize and add elements to a list. Additionally we also looked at how we would access elements of a list and loop through them. In this chapter we will look at some useful list methods.

1. Remove()

The Remove method removes the first instance of an element from a list.

Syntax: exampleList.remove(targetElement);

cs

index.cs

In case no such element is found, it simply doesn't do anything:

cs

index.cs

2. RemoteAt()

The RemoveAt method removes an element at a specific index.

Syntax: exampleList.RemoveAt(index);

cs

index.cs

3. Clear

The Clear method simply removes all elements from a list. It takes no arguments.

Syntax: exampleList.Clear();

cs

index.cs

4. Insert

The Insert method inserts an element in the list at a specified index.

Syntax: exampleList.Insert(index, elementToInsert);

cs

index.cs

We can omit the curly braces {} in case there's only a single statement inside an if-condition, a for-loop, or a foreach loop. For-example:

cs

index.cs

The above snippet is the same as:

cs

index.cs

If there's already an element at the specified index, it is pushed to the right, and so are the remaining elements of the array after it - in case there are any. The following diagram illustrates the process:

5. Contains()

The Contains method checks if a list contains a specific element. It returns a boolean value (true or false).

Syntax: exampleList.Contains(element);

cs

index.cs

6. IndexOf()

The IndexOf method returns the index of the first occurrence of an element in a list.

Syntax: exampleList.IndexOf(element);

cs

index.cs

If the element doesn't exist in the list, it simply returns -1:

The indexOf method is especially useful when we want to access an element by index but we won't know it's index. For-example in the vowels, we want to access the element o by index and change it to capital O:

cs

index.cs

1. What does the `Remove()` method do?
2. What is the fastest way of checking if a list contains a specific element?
3. Which method is used for removing all elements from a list?
4. What will be the output of the following code?

What does the Remove() method do?

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

What is the fastest way of checking if a list contains a specific element?

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

Which method is used for removing all elements from a list?

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

What will be the output of the following code?

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

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

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