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
-
Namespaces:
-
using System;
andusing System.Collections.Generic;
are included to use basic features and collections like lists.
-
-
Main Class:
- The
Program
class contains all the logic for our application.
- The
-
Task List:
- We use a
List<Task>
to store our tasks. This allows dynamic addition and removal of tasks.
- We use a
-
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.
- A
-
Adding Tasks:
- The
AddTask
method prompts the user to enter a task description, which is then added to the list.
- The
-
Displaying Tasks:
- The
ShowTasks
method shows all tasks with their completion status. If there are no tasks, it informs the user.
- The
-
Marking Tasks as Completed:
- In
MarkTaskAsDone
, the user selects a task by number, and it is marked as completed.
- In
-
Deleting Tasks:
- The
DeleteTask
method allows users to remove a task from the list by selecting its number.
- The
-
Task Class:
- The
Task
class holds the description and status (completed or not) of a task.
- The
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;
}
}
}
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)