Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Exploring Lists | Working with Lists and Arrays
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Lists & Collections

bookExploring Lists

When working with collections of data in C#, you have options like arrays and lists. Arrays are fixed in size once created, meaning you cannot add or remove elements after initialization. In contrast, List<T> is a generic collection that allows you to store a sequence of elements of a specific type, but with dynamic sizing. This means you can add or remove items at any time, and the list will automatically resize as needed.

Some of the most commonly used methods with List<T> include:

  • Add: inserts an element at the end of the list;
  • Remove: deletes the first occurrence of a specific element;
  • Count: returns the number of items currently in the list;
  • Clear: removes all items from the list;
  • Contains: checks if a specific item exists in the list.
Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> fruits = new List<string>(); // Add items fruits.Add("Apple"); fruits.Add("Banana"); fruits.Add("Cherry"); // Remove an item fruits.Remove("Banana"); // Print the list Console.WriteLine("Fruits in the list:"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } // Print the count Console.WriteLine("Total fruits: " + fruits.Count); } } }

In the code above, you create a new List<string> called fruits. You use the Add method to insert "Apple", "Banana", and "Cherry" into the list. The Remove method deletes "Banana" from the list. To display the contents, you use a foreach loop to print each fruit. Finally, the Count property shows how many items remain in the list after removal. These methods—Add, Remove, and Count—are essential for managing the contents of a list and understanding how lists differ from arrays.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> colors = new List<string>(); colors.Add("Red"); colors.Add("Green"); colors.Add("Blue"); Console.WriteLine("Colors in the list:"); foreach (string color in colors) { Console.WriteLine(color); } } } }

1. What is a key advantage of List over arrays?

2. Which method is used to add an item to a List?

3. Complete the code so that it removes the value 2 from the list.

question mark

What is a key advantage of List over arrays?

Select the correct answer

question mark

Which method is used to add an item to a List?

Select the correct answer

question-icon

Complete the code so that it removes the value 2 from the list.

();

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain more about the differences between arrays and lists in C#?

How do I use the Add and Remove methods with List<T>?

What are some common scenarios where using a list is better than an array?

bookExploring Lists

Glissez pour afficher le menu

When working with collections of data in C#, you have options like arrays and lists. Arrays are fixed in size once created, meaning you cannot add or remove elements after initialization. In contrast, List<T> is a generic collection that allows you to store a sequence of elements of a specific type, but with dynamic sizing. This means you can add or remove items at any time, and the list will automatically resize as needed.

Some of the most commonly used methods with List<T> include:

  • Add: inserts an element at the end of the list;
  • Remove: deletes the first occurrence of a specific element;
  • Count: returns the number of items currently in the list;
  • Clear: removes all items from the list;
  • Contains: checks if a specific item exists in the list.
Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> fruits = new List<string>(); // Add items fruits.Add("Apple"); fruits.Add("Banana"); fruits.Add("Cherry"); // Remove an item fruits.Remove("Banana"); // Print the list Console.WriteLine("Fruits in the list:"); foreach (string fruit in fruits) { Console.WriteLine(fruit); } // Print the count Console.WriteLine("Total fruits: " + fruits.Count); } } }

In the code above, you create a new List<string> called fruits. You use the Add method to insert "Apple", "Banana", and "Cherry" into the list. The Remove method deletes "Banana" from the list. To display the contents, you use a foreach loop to print each fruit. Finally, the Count property shows how many items remain in the list after removal. These methods—Add, Remove, and Count—are essential for managing the contents of a list and understanding how lists differ from arrays.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { List<string> colors = new List<string>(); colors.Add("Red"); colors.Add("Green"); colors.Add("Blue"); Console.WriteLine("Colors in the list:"); foreach (string color in colors) { Console.WriteLine(color); } } } }

1. What is a key advantage of List over arrays?

2. Which method is used to add an item to a List?

3. Complete the code so that it removes the value 2 from the list.

question mark

What is a key advantage of List over arrays?

Select the correct answer

question mark

Which method is used to add an item to a List?

Select the correct answer

question-icon

Complete the code so that it removes the value 2 from the list.

();

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2
some-alt