Loading...

C# List Methods

Keep a List<int> named bag. Add 3, then add 8. Call bag.Remove(3);. Write bag[0] and then write bag.Count. Keep the work in Pine with Main. Keep using System.Collections.Generic; under using System;. Run with dotnet run. The screen should show two lines: 8 then 1. Remove drops the first matching value. The remaining value sits at index 0. Count shrinks to 1. An earlier check on this page prints bag.Contains(8) as True before you remove. Keep the starting public class named Pine.

Ask, drop, and find on the same bag

On the list page you created a bag, called Add, read an index, and read Count. Those habits stay. This page teaches three more actions on the same kind of bag: Contains asks whether a value is present, Remove drops the first match, and IndexOf reports the slot of a value when it exists. You are not building a new type. You are asking the list more questions and changing what it holds.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any leftover that only prints bag[0] and bag.Count after two Add calls with no Remove — that was the list-page checkpoint. Delete any Box b = new Box(4); leftover. Delete any ReadLine line. The file you keep has no wait for typing. A clean Main makes the two-line screen after Remove easy to trust.
At the top keep using System;. Keep using System.Collections.Generic;. Inside Main, write List<int> bag = new List<int>();, then bag.Add(3);, then bag.Add(8);. For a first check, write Console.WriteLine(bag.Contains(8));. Save and run that short shape, then extend to Remove and the two writes as the kept demo. The Contains run is only a checkpoint; the page's kept answer after Remove is still 8 then 1.
Contains returns a bool. When the value is in the list, the screen shows True. When it is not, the screen shows False. Do not treat Contains as a count. Count is still the length. Contains only answers yes or no for one value you pass in.

Contains after two Adds

csharp
using System;
using System.Collections.Generic;

public class Pine
{
    public static void Main()
    {
        List<int> bag = new List<int>();
        bag.Add(3);
        bag.Add(8);
        Console.WriteLine(bag.Contains(8));
    }
}

Pine removes one value then writes what remains

Inside Main, after the two Add calls, write bag.Remove(3);. Then write bag[0] so the screen shows the value that stayed. Then write bag.Count so the screen shows the new length. You are not only printing Contains on the kept file. The kept demo drops 3 and shows what is left.

Remove then bag index and Count

csharp
using System;
using System.Collections.Generic;

public class Pine
{
    public static void Main()
    {
        List<int> bag = new List<int>();
        bag.Add(3);
        bag.Add(8);
        bag.Remove(3);
        Console.WriteLine(bag[0]);
        Console.WriteLine(bag.Count);
    }
}

Read the kept file from the top. using System; prepares console names. using System.Collections.Generic; prepares List. public class Pine is the starter. public static void Main() is where the runtime starts. new List<int>() builds an empty list. The two Add calls store 3 then 8. Remove(3) drops the first 3. After that drop, index 0 holds 8. Count is 1. Writing those two values prints 8 then 1.
Run it with dotnet run. The program writes two lines and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the list-method work. The screen shows 8 then 1. If the screen shows 3 then 2, you are still on the list-page demo without Remove — put bag.Remove(3); back before the writes. If the screen shows only True, you are on the Contains checkpoint; switch to the Remove file for the kept answer. If the build cannot find List, restore using System.Collections.Generic;.
If you call Remove(9) when 9 was never added, the list stays unchanged and Remove returns false — this page's kept demo removes a value that is present. If you write bag[1] after the Remove on this demo, the run can throw because only one value remains. Stay with bag[0] and Count after the Remove.
IndexOf finds a slot. After the two Adds and before Remove, bag.IndexOf(8) is 1. After Remove of 3, bag.IndexOf(8) is 0. If the value is missing, IndexOf returns -1. You do not need a third code file for IndexOf on this page; the table and prose carry that check. Keep IndexOf in mind when you ask where a value sits instead of only whether it sits.

Contains, Remove, and IndexOf side by side

Contains answers presence with True or False. Remove changes the list when the value is found. IndexOf answers with a slot number or -1. All three work on the same List<int> bag you already know how to create and fill with Add.
A leftover that only prints bag[0] and bag.Count after two Adds without Remove is the previous page's checkpoint. For this page keep the Remove line in the main demo. Mixing a Box leftover into Main can print a box size instead of the list lines — keep Box work out while you practice list methods.

📊 Contains Remove IndexOf

Call What it answers on this page
bag.Contains(8) whether 8 is present (True / False)
bag.Remove(3) drops the first 3 when it is there
bag.IndexOf(8) slot of 8, or -1 if missing

What this leaves for dictionary

This page asks the bag with Contains, drops a value with Remove, and finds a slot with IndexOf. A later page — dictionary — stores pairs of keys and values, not only a growable sequence of ints. Stay with list methods on one List<int> here. Do not invent every list method on this page; the lesson goal is Contains, Remove, and IndexOf on the bag you already built.
Do not nest a second Main. Do not leave Pack / Box in the file. If the screen shows True alone, you are on the Contains file — use the Remove file when you want 8 then 1. If Count is still 2 after you expected a remove, the Remove line may be missing or the value you passed was never in the list. When dotnet run prints 8 then 1 from the Remove file, you have the checkpoint this page asks for.

You can write List<int> bag = new List<int>();, call bag.Add(3); and bag.Add(8);, then bag.Remove(3);, then write bag[0] and bag.Count. Run with dotnet run. The screen shows 8 then 1. Contains(8) was True before the drop. IndexOf would report the slot of a value that remains. Think of these calls as questions and edits on the same bag. Pine stays the door you open with dotnet run. The next page is dictionary, where storage shifts from a list of values to keyed pairs.
Keep using System.Collections.Generic;. Keep <int> on the list type. Keep leftover Add-only demos out of the kept Remove file so the screen cannot show 3 then 2 by mistake. Those habits are the checkpoint before you move on.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top