Loading...

C# List

Create a List<int> named bag with new List<int>(). Add 3, then add 8. Write bag[0] and then write bag.Count. Keep the work in Pine with Main. Add using System.Collections.Generic; under using System; so the short name List works. Run with dotnet run. The screen should show two lines: 3 then 2. A list grows when you Add. An index reads one stored value. Count is how many values are in the list. Keep the starting public class named Pine.

A growable sequence

On the using page you built one Box with a single size. Here you store several int values in one List. The list starts empty. Each Add appends at the end. The first value sits at index 0. The number of values is Count, not a field you invent by hand. Think of the list as a bag: you drop numbers in, then ask what sits in the first slot and how many drops you made.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any Box b = new Box(4); leftover if that using demo is still all you have in Main. Delete any namespace Pack / Box leftover. Delete any ReadLine line. The file you keep has no wait for typing. A clean Main makes the two-line screen easy to trust.
At the top keep using System;. Add using System.Collections.Generic;. Inside Main, write List<int> bag = new List<int>();. Then bag.Add(3);. Then write Console.WriteLine(bag[0]); for a first check. Save before you run that short shape, then extend to two Adds and Count as the kept demo. That first one-Add run is only a checkpoint; the page’s kept answer is still 3 then 2.
List needs the generic argument <int> on this page so every slot holds an int. Do not write a plain List bag without the type argument in the teaching file. The angle brackets are part of the type name you type in the editor.

List one Add

csharp
using System;
using System.Collections.Generic;

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

Pine adds twice and writes Count

Inside Main, after the empty list, write bag.Add(3); then bag.Add(8);. Write bag[0] first so the screen shows the first value. Then write bag.Count so the screen shows how many values you stored. You are not writing Box b = new Box(4); on this page. That was the using lesson.

Pine two Adds 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);
        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 first Add stores 3 at index 0. The second Add stores 8 at index 1. Writing bag[0] prints 3. Writing bag.Count prints 2.
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 work. The screen shows 3 then 2. If the screen shows 4, a leftover Box demo may still be running. If the build cannot find List, using System.Collections.Generic; may be missing — put it back.
If you write bag[2] before a third Add, the run can throw because that index does not exist yet. Stay with bag[0] and Count on this page’s main demo. If you forget both Add calls, Count is 0 and bag[0] fails — add the values first.

Compared with Box and with a fixed array habit

On the using page, one object held one size. A list holds many values under one name. A leftover Box b = new Box(4); Console.WriteLine(b.size); is a different lesson. For this page keep List<int> bag and the Add calls. Mixing both demos in one Main can make the screen print a box size instead of the list lines — remove the box work while you practice List.
Arrays fix a length when you create them. List grows when you Add without declaring a new size each time. This page does not replace every array. It shows the growable bag shape you will extend on the list-methods page. Stay with int values here so the index and Count numbers stay easy to read on the screen.

📊 index vs Count

Expression Meaning on this page
bag[0] the value in the first slot
bag.Count how many values the list holds now

Three Adds still share one list

You can Add a third value and read another index. Write bag.Add(3);, bag.Add(8);, bag.Add(5);, then write bag[1] and bag.Count. The middle value is 8. The count becomes 3.

Pine three 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);
        bag.Add(5);
        Console.WriteLine(bag[1]);
        Console.WriteLine(bag.Count);
    }
}

Run again with dotnet run. The screen should show 8 then 3. If it still shows 3 then 2, you are on the two-Add demo — that first checkpoint is fine; use the three-Add file when you want the second check. If bag[1] throws, an Add may be missing before the write.
Writing List<int> other = bag; does not copy the values into a second list. Both names would share the same list object. For this page, one bag and clear Add calls are enough.

What this leaves for list-methods

This page builds a list, adds values, reads an index, and reads Count. A later page — list-methods — shows more list actions such as removing or searching. Stay with create, Add, index, and Count here. Do not invent every list method on this page; the lesson goal is the growable bag itself. When dotnet run prints 3 then 2 from the two-Add file, you have the checkpoint this page asks for.
Do not nest a second Main. Do not leave Pack / Box in the file. If the build cannot find List, check using System.Collections.Generic;. If the screen shows only one line, you may have forgotten the second WriteLine — keep both bag[0] and bag.Count on the two-Add demo. If the screen shows 8 then 3, you are on the three-Add file; that is fine for the second check, then return to the two-Add shape when you want the quiz answer on screen.

You can write List<int> bag = new List<int>();, call bag.Add(3); and bag.Add(8);, then write bag[0] and bag.Count. Run with dotnet run. The screen shows 3 then 2. The list grew. The index read the first value. Count reported the length. Think of List as a bag you can drop numbers into without fixing the size up front. Pine stays the door you open with dotnet run. The next page is list-methods, where the same bag learns more actions.
Keep using System.Collections.Generic;. Keep <int> on the list type. Keep leftover Box demos out of Main so the screen cannot show a single box size 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

❓
5
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