DEV Community

Kerimova_Manzura
Kerimova_Manzura

Posted on

Task Manager (To-Do List).

Simple To-Do List App in C

This app allows users to add tasks, view their list, mark tasks as completed, and delete tasks. It's a great project for beginners to practice basic C# concepts.

Key Components of the Code

  1. Namespaces:

    • using System; and using System.Collections.Generic; are included to use basic features and collections like lists.
  2. Main Class:

    • The Program class contains all the logic for our application.
  3. Task List:

    • We use a List<Task> to store our tasks. This allows dynamic addition and removal of tasks.
  4. Main Loop:

    • A while loop keeps the application running until the user chooses to exit. It clears the console and presents a menu of options.
  5. Adding Tasks:

    • The AddTask method prompts the user to enter a task description, which is then added to the list.
  6. Displaying Tasks:

    • The ShowTasks method shows all tasks with their completion status. If there are no tasks, it informs the user.
  7. Marking Tasks as Completed:

    • In MarkTaskAsDone, the user selects a task by number, and it is marked as completed.
  8. Deleting Tasks:

    • The DeleteTask method allows users to remove a task from the list by selecting its number.
  9. Task Class:

    • The Task class holds the description and status (completed or not) of a task.

Sample Code

Here’s a simplified version of the code:

using System;
using System.Collections.Generic;

namespace TodoListApp
{
    class Program
    {
        static List<Task> tasks = new List<Task>();

        static void Main(string[] args)
        {
            bool exit = false;

            while (!exit)
            {
                Console.Clear();
                Console.WriteLine("Task Manager");
                Console.WriteLine("1. Add a task");
                Console.WriteLine("2. Show task list");
                Console.WriteLine("3. Mark task as completed");
                Console.WriteLine("4. Delete a task");
                Console.WriteLine("5. Exit");

                string input = Console.ReadLine();

                switch (input)
                {
                    case "1": AddTask(); break;
                    case "2": ShowTasks(); break;
                    case "3": MarkTaskAsDone(); break;
                    case "4": DeleteTask(); break;
                    case "5": exit = true; break;
                    default: Console.WriteLine("Invalid input. Try again."); break;
                }
            }
        }

        // Other methods (AddTask, ShowTasks, MarkTaskAsDone, DeleteTask) go here...
    }

    class Task
    {
        public string Description { get; }
        public bool IsDone { get; set; }

        public Task(string description)
        {
            Description = description;
            IsDone = false;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

This simple To-Do List application is an excellent way to practice your C# skills. You can extend its functionality by adding features like saving tasks to a file or categorizing tasks. Enjoy coding!

Top comments (0)