Exploring 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
1234567891011121314151617181920212223242526272829303132using 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
1234567891011121314151617181920212223using 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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.76
Exploring Lists
Swipe to show 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
1234567891011121314151617181920212223242526272829303132using 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
1234567891011121314151617181920212223using 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.
Thanks for your feedback!