Learning C# by creating a To-do application can be a very useful exercise for beginners. Using this application, a user can add tasks and view a list of all items inside their To-do list. They can also choose to delete items from the list.

A To-do application covers many programming fundamentals. This includes handling user input, storing data in arrays, using conditional statements, and working with loops. You can create a simple To-do application using a C# console application in Visual Studio.

How to Create a New Console Application

You can view the full source code for this To-do list project on GitHub.

You can create a To-do application in lots of different programming languages. It can be difficult to decide which language to choose, especially if you are a beginner. However, there are several practical reasons to learn C#.

Start by creating a new Visual Studio C# console application.

  1. Open the Visual Studio application on your computer.
  2. Click on Create a new project:
    Create new project page
  3. Choose C# Console Application, and click Next:
    Create new console app project
  4. Give the project a name and a location to store the solution:
    Create project name and location page
  5. Keep the Target Framework at the default value and click Create:
    Create project choose target framework page

How to Add a Task to the To-Do List

After creating the new console application, a generated "Hello World" program will appear by default. Modify the code to create a list that you can add tasks to.

Since this is a console application, you can use print statements to ask the user what information they want to add to the To-do list.

  1. At the top of the file, import the "System.Collections.Generic" namespace. This will allow you to create List objects in your program:
            using System.Collections.Generic;
        
  2. Inside the Main() function, remove the default "Hello World" code. Declare a new list to store the To-do list's tasks:
            List<string> toDoList = new List<string>();
        
  3. Add a while loop that will run indefinitely until the user ends the program:
            while (true) {

    }
  4. Above the Main() function, add an enum for the list of possible user actions:
            enum UserChoice { 
     AddTask = 1,
     Exit
    }
  5. Inside the while loop in the Main() function, display the list of possible actions the user can choose from. The user can then enter a number to complete the action. For example, the user can press "1" if they want to add a new task to the list.
            Console.WriteLine("1. Add task");
    Console.WriteLine("2. Exit");
    int choice = int.Parse(Console.ReadLine());
  6. If the user presses "1" to add a task, add another prompt to ask the user what the task name is. Add the user's input into the array of tasks. Instead of an if-statement, you can also use a C# switch statement instead.
            if (choice == (int)UserChoice.AddTask) {
     Console.Write("Enter task: ");
     string task = Console.ReadLine();
     toDoList.Add(task);
     Console.Clear();
     Console.WriteLine("Task added successfully!");
    }
  7. If the user exits the application, break out of the while loop:
            else if (choice == (int)UserChoice.Exit) { 
     break;
    }
  8. Click on the green play button at the top of the Visual Studio application:
    Run Visual Studio application green button
  9. Select the first option by entering "1". Enter a task into the To-do list:
    Add task in todo list

How to Display the Tasks in the To-Do List

Before asking the user if they want to add a task, display the current tasks in the To-do list.

  1. Inside the while loop in the main function, add an if-statement to check if the To-do list is not empty:
            while (true) { 
     if (toDoList.Count > 0) {
     
     }

     Console.WriteLine("1. Add task");
     Console.WriteLine("2. Exit");
    }
  2. If the To-do list has items inside it, loop through each item in the "toDoList" array to display the name of the task in bullet point format:
            if (toDoList.Count > 0) { 
     Console.WriteLine("To-do List:");

     for (int i = 0; i < toDoList.Count; i++) {
       Console.WriteLine("- " + toDoList[i]);
     }

     Console.WriteLine("");
    }
  3. Add an else condition to the if-statement. If the list is empty, display a different message:
            else { 
     Console.WriteLine("You currently have no tasks in your To-do list.");
     Console.WriteLine("");
    }
  4. Click on the green play button at the top of the Visual Studio application.
  5. Add some items to the list. As you add an item, the console updates to display the new items in your To-do list:
    View items in to do list

How to Delete a Task From the To-Do List

When deleting a task, re-show the list of tasks to the user and ask them which task they would like to delete.

  1. Update the UserChoice enum at the top of the file to include the action of deleting a task:
            enum UserChoice { 
     AddTask = 1,
     DeleteTask,
     Exit
    }
  2. Update the list of actions displayed to the user to show the new delete option:
            Console.WriteLine("1. Add task");
    Console.WriteLine("2. Delete task");
    Console.WriteLine("3. Exit");
  3. Inside the while loop in the Main() function, add a new else-if condition for when the user chooses the delete option:
            else if (choice == (int)UserChoice.DeleteTask) {

    }
  4. Inside, check if the To-do list is empty. If not, re-display the list of tasks with a number in front of each item. This allows the user to enter the number of the task they would like to delete:
            if (toDoList.Count > 0) { 
     Console.WriteLine("Enter the number of the task you want to delete:");

     for (int i = 0; i < toDoList.Count; i++) {
       Console.WriteLine("(" + (i + 1) + ") " + toDoList[i]);
     }
    }
  5. After the for-loop, get the user's input of what item they would like to delete. Using the input, get the corresponding index of the item, and use the RemoveAt() function to delete the task item:
            int taskNum = int.Parse(Console.ReadLine());
    taskNum--;

    if (taskNum >= 0 && taskNum < toDoList.Count) {
     toDoList.RemoveAt(taskNum);
     Console.Clear();
     Console.WriteLine("Task deleted successfully!");
     Console.WriteLine("");
    }
  6. Add an else condition to the if-statement to check that the user didn't enter an invalid task number:
            else { 
     Console.Clear();
     Console.WriteLine("Invalid task number.");
     Console.WriteLine("");
    }
  7. Click on the green play button at the top of the Visual Studio application.
  8. Click on option "1" to add items in your To-do list.
  9. Click on option "2" to delete an existing item in your To-do list.
    Delete item in to do list
    Delete item in to do list

Learn to Program by Making Simple Apps

You should have successfully managed to create a simple To-do application using the console. You can continue to expand your knowledge by creating apps that require a user interface.

Try designing and building both the UI and logic for a C# application using Windows Forms. One example project you can try out is a simple file manager application.