0
0
C Sharp (C#)programming~5 mins

List methods (Add, Remove, Find, Sort) in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Lists help you keep many items in order. You use methods like Add, Remove, Find, and Sort to change or search the list easily.

You want to keep a list of your friends' names and add new friends.
You want to remove a name from a list when someone moves away.
You want to find if a certain name is in your list.
You want to sort a list of numbers or names to find things faster.
Syntax
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> items = new List<string>();

        // Add an item
        items.Add("apple");

        // Remove an item
        items.Remove("apple");

        // Find an item
        string foundItem = items.Find(item => item == "apple");

        // Sort the list
        items.Sort();
    }
}

Add adds one item to the end of the list.

Remove deletes the first matching item from the list.

Examples
Adding and removing from an empty list works fine.
C Sharp (C#)
List<int> numbers = new List<int>();
numbers.Add(10); // List has one item: 10
numbers.Remove(10); // List is empty now
Removing the only item leaves the list empty.
C Sharp (C#)
List<string> fruits = new List<string> { "apple" };
fruits.Remove("apple"); // List becomes empty
Find returns the first item that matches the condition.
C Sharp (C#)
List<string> colors = new List<string> { "red", "blue", "green" };
string found = colors.Find(color => color == "red"); // found is "red"
Sort arranges the list items in ascending order.
C Sharp (C#)
List<int> scores = new List<int> { 30, 10, 20 };
scores.Sort(); // List becomes 10, 20, 30
Sample Program

This program shows how to add, remove, find, and sort items in a list. It prints the list before and after each operation so you can see the changes clearly.

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> shoppingList = new List<string>();

        Console.WriteLine("Initial list:");
        PrintList(shoppingList);

        // Add items
        shoppingList.Add("Milk");
        shoppingList.Add("Bread");
        shoppingList.Add("Eggs");

        Console.WriteLine("After adding items:");
        PrintList(shoppingList);

        // Remove an item
        shoppingList.Remove("Bread");

        Console.WriteLine("After removing 'Bread':");
        PrintList(shoppingList);

        // Find an item
        string foundItem = shoppingList.Find(item => item == "Eggs");
        Console.WriteLine($"Found item: {foundItem}");

        // Sort the list
        shoppingList.Sort();

        Console.WriteLine("After sorting:");
        PrintList(shoppingList);
    }

    static void PrintList(List<string> list)
    {
        if (list.Count == 0)
        {
            Console.WriteLine("(empty list)");
        }
        else
        {
            foreach (string item in list)
            {
                Console.WriteLine(item);
            }
        }
        Console.WriteLine();
    }
}
OutputSuccess
Important Notes

Time complexity: Add is usually fast (amortized O(1)), Remove and Find take O(n) time because they may check many items, Sort takes O(n log n).

Space complexity: List uses extra space to store items, grows as you add more.

Common mistake: Trying to remove an item not in the list does nothing and does not cause error.

Use Add to add items, Remove to delete specific items, Find to search, and Sort to order the list.

Summary

Use Add to put new items at the end of the list.

Use Remove to delete the first matching item.

Use Find to get the first item that matches a condition.

Use Sort to arrange items in order.