Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
List methods (Add, Remove, Find, Sort)
📖 Scenario: You are managing a small library's book collection. You want to keep track of book titles, add new books, remove old ones, find specific books, and keep the list sorted alphabetically.
🎯 Goal: Build a simple C# program that uses a list of book titles and applies common list methods: Add, Remove, Find, and Sort.
📋 What You'll Learn
Create a list of strings called books with exact initial titles
Add a new book title to the books list
Remove a specific book title from the books list
Find a book title in the books list using Find
Sort the books list alphabetically
Print the final sorted list of books
💡 Why This Matters
🌍 Real World
Managing collections of items like books, products, or contacts often requires adding, removing, searching, and sorting data.
💼 Career
These list methods are fundamental in many programming jobs, especially when working with collections of data in applications.
Progress0 / 4 steps
1
Create the initial list of books
Create a list of strings called books with these exact titles: "The Hobbit", "1984", "Brave New World", "Fahrenheit 451".
C Sharp (C#)
Hint
Use List<string> and initialize it with the four book titles inside curly braces.
2
Add a new book to the list
Add the book title "Animal Farm" to the books list using the Add method.
C Sharp (C#)
Hint
Use books.Add("Animal Farm") to add the new book.
3
Remove a book from the list
Remove the book titled "1984" from the books list using the Remove method.
C Sharp (C#)
Hint
Use books.Remove("1984") to remove the book.
4
Find and sort the books, then print the list
Use the Find method on books to find the book titled "Animal Farm" and store it in a variable called foundBook. Then sort the books list alphabetically using Sort. Finally, print each book title on its own line using a foreach loop.
C Sharp (C#)
Hint
Use string foundBook = books.Find(book => book == "Animal Farm") to find the book. Then call books.Sort(). Use a foreach loop to print each book.
Practice
(1/5)
1. Which List method in C# is used to add a new item to the end of the list?
easy
A. Sort
B. Remove
C. Find
D. Add
Solution
Step 1: Understand the purpose of Add
The Add method appends a new element to the end of a list.
Step 2: Compare with other methods
Remove deletes items, Find searches, and Sort arranges items, so they don't add new items.
Final Answer:
Add -> Option D
Quick Check:
Add method adds items [OK]
Hint: Add puts new items at the list's end [OK]
Common Mistakes:
Confusing Remove with Add
Thinking Find adds items
Assuming Sort adds items
2. Which of the following is the correct syntax to remove the first occurrence of "apple" from a List<string> named fruits?
easy
A. fruits.RemoveAt("apple");
B. fruits.Delete("apple");
C. fruits.Remove("apple");
D. fruits.RemoveItem("apple");
Solution
Step 1: Identify the correct method name
The method to remove an item by value is Remove, so fruits.Remove("apple") is correct.
Step 2: Check method parameters and usage
RemoveAt requires an index, not a string. Delete and RemoveItem are not valid List methods.
Final Answer:
fruits.Remove("apple"); -> Option C
Quick Check:
Remove("apple") removes first matching item [OK]
Hint: Use Remove with the item value to delete it [OK]
Common Mistakes:
Using RemoveAt with a string argument
Using non-existent methods like Delete or RemoveItem
Confusing Remove with Add
3. What will be the output of the following C# code?
var numbers = new List<int> {5, 3, 8, 1};
numbers.Sort();
Console.WriteLine(string.Join(",", numbers));
medium
A. 5,3,8,1
B. 1,3,5,8
C. 8,5,3,1
D. 3,5,1,8
Solution
Step 1: Understand what Sort does
Sort arranges the list items in ascending order.
Step 2: Apply Sort to the list
The list {5, 3, 8, 1} sorted ascending becomes {1, 3, 5, 8}.
Final Answer:
1,3,5,8 -> Option B
Quick Check:
Sort orders numbers ascending [OK]
Hint: Sort arranges numbers from smallest to largest [OK]
Common Mistakes:
Assuming Sort reverses the list
Confusing Sort with Find
Expecting original order after Sort
4. Identify the error in this code snippet:
var fruits = new List<string> {"apple", "banana", "cherry"};
fruits.RemoveAt("banana");
medium
A. RemoveAt expects an index, not a string
B. RemoveAt cannot be used on List<string>
C. RemoveAt removes all matching items
D. RemoveAt adds an item instead of removing
Solution
Step 1: Check RemoveAt parameter type
RemoveAt requires an integer index, but "banana" is a string.
Step 2: Understand method behavior
Using a string causes a compile-time error because the argument type is wrong.
Final Answer:
RemoveAt expects an index, not a string -> Option A
Quick Check:
RemoveAt needs index integer [OK]
Hint: RemoveAt uses index number, not item value [OK]
Common Mistakes:
Passing item value instead of index to RemoveAt
Thinking RemoveAt removes all matches
Confusing RemoveAt with Remove
5. Given a List<int> numbers = new List<int> {4, 7, 2, 9, 3}; which code snippet correctly finds the first number greater than 5 and removes it from the list?
hard
A. var num = numbers.Find(n => n > 5);
numbers.Remove(num);
B. numbers.RemoveAt(numbers.Find(n => n > 5));
C. numbers.Remove(numbers.FindIndex(n => n > 5));
D. numbers.Remove(numbers.Find(n => n < 5));
Solution
Step 1: Use Find to get first number > 5
Find returns the first element matching the condition n > 5, which is 7.
Step 2: Remove that number from the list
Remove(num) deletes the first occurrence of 7 from the list.
Final Answer:
var num = numbers.Find(n => n > 5); numbers.Remove(num); -> Option A
Quick Check:
Find returns item, Remove deletes it [OK]
Hint: Find returns item; Remove deletes that item [OK]
Common Mistakes:
Passing Find result directly to RemoveAt (wrong type)
Using FindIndex result with Remove (expects item, not index)
Searching for wrong condition (n < 5 instead of n > 5)