Arrays and Lists in C#
Imagine you are organizing your school bag. One pocket for pens, another for notebooks, another for snacks. But what if you needed to store 10 pens, 5 notebooks, or 20 sticky notes?
You wouldn’t create 20 separate pockets you’d put similar items together in one big pocket.
In programming, that “big pocket” is called an array or list. These allow your programs to store multiple values in one place, something essential for real apps.
Today, you’ll learn how to store, access, update, and manage collections of items… and then build your first Mini To-Do List App.
The Project: Mini To-Do List App
You will create a small app where users can:
- Add tasks
- View all tasks
- Remove tasks
- Search tasks
This is your first step toward building real applications that manage data.
Understanding Arrays
An array stores multiple values of the same type.
Example:
1int[] numbers = new int[5];
This creates space for 5 integers. Assign values:
1 2numbers[0] = 10; numbers[1] = 20;
Access values:
1Console.WriteLine(numbers[0]); // prints 10
Arrays are great when:
- You know the exact number of items
- You don’t plan to resize the collection
But sometimes you don’t know how many items you’ll need. That’s where lists come in.
List<T> for Dynamic Storage
A List<T> is like an expandable pocket. It grows and shrinks automatically as you add or remove items.
Example:
1List<string> tasks = new List<string>();
Add items
1 2tasks.Add("Buy milk"); tasks.Add("Do homework");
Remove items
1tasks.Remove("Buy milk");
Search items
1tasks.Contains("Do homework"); // returns true or false
Access items
1Console.WriteLine(tasks[0]);
Lists are perfect for to-do apps, menus, playlists, user data, almost everything.
Let’s Build the Mini To-Do List App
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63using System; using System.Collections.Generic; class Program { static void Main() { List<string> tasks = new List<string>(); int choice; while (true) { Console.WriteLine("========================"); Console.WriteLine(" MINI TO-DO LIST"); Console.WriteLine("========================"); Console.WriteLine("1. Add Task"); Console.WriteLine("2. View All Tasks"); Console.WriteLine("3. Remove Task"); Console.WriteLine("4. Search Task"); Console.WriteLine("5. Exit"); Console.WriteLine("Choose an option:"); choice = Convert.ToInt32(Console.ReadLine()); if (choice == 1) { Console.WriteLine("Enter task:"); string task = Console.ReadLine(); tasks.Add(task); Console.WriteLine("Task added!"); } else if (choice == 2) { Console.WriteLine("Your Tasks:"); foreach (string t in tasks) { Console.WriteLine("- " + t); } } else if (choice == 3) { Console.WriteLine("Enter task to remove:"); string taskToRemove = Console.ReadLine(); tasks.Remove(taskToRemove); Console.WriteLine("Task removed (if it existed)."); } else if (choice == 4) { Console.WriteLine("Enter task to search:"); string search = Console.ReadLine(); if (tasks.Contains(search)) Console.WriteLine("Task found!"); else Console.WriteLine("Task not found."); } else if (choice == 5) { Console.WriteLine("Exiting..."); break; } } } }
How It Works
- A List<string> stores tasks dynamically
- The menu uses a loop so the app never stops until the user chooses Exit
- Add/Remove/Search operations are performed directly on the list
- The foreach loop prints tasks neatly
Try running this in the DevsCall AI Online Runner and add your own tasks.
Learn Together with AI
Try prompts like:
- “Add task numbers (1,2,3…) to the to-do list output.”
- “Add an option to edit a task.”
- “Explain the difference between arrays and lists in simple words.”
- “Rewrite this app using arrays instead of lists.”
AI will help you customize and upgrade your project.
Practice Time
Build a program that:
- Creates a list of your 5 favorite movies
- Allows the user to:
- Add a new movie
- Remove a movie
- Search for a movie
- Prints all movies in a neat list
Sample output:
1 2 3 4Favorite Movies: 1. Inception 2. Avatar 3. Coco
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22using System; using System.Collections.Generic; class Program { static void Main() { List<string> movies = new List<string>() { "Inception", "Avatar", "Coco" }; Console.WriteLine("Enter a movie to add:"); string newMovie = Console.ReadLine(); movies.Add(newMovie); Console.WriteLine("Enter a movie to search:"); string search = Console.ReadLine(); Console.WriteLine(movies.Contains(search) ? "Found!" : "Not found."); Console.WriteLine("\nAll Movies:"); foreach (string m in movies) Console.WriteLine("- " + m); } }
Great job, you’ve learned how to store, manage, and search multiple values!
Frequently Asked Questions
An array is a fixed-size collection that stores multiple values of the same type, such as numbers or strings.
Use arrays when you know the exact number of items you need and the size will not change during the program.
Arrays use .Length, while lists use .Count to show number of elements.
Lists are usually easier for beginners because they handle resizing automatically.
No. You can run all array and list examples directly in the DevsCall AI Online Runner.
Still have questions?Contact our support team