We will develop a console application which could have a friendly UX and persistent data storage. In this beginner project, we will demonstrate various object-oriented programming features and list manipulations along with file handling. Our end project would something like this.

First, create a new console application and name it, such as - to-do app.
A new project will start. First, we will start off by creating the persistent data communication layer. For this, we are maintaining a file where our to-do task would be stored. We will create a class named DataPersistence for that.
- public class DataPersistence
- {
- //constructor which would make the file if the file does not exist
- public DataPersistence()
- {
- if (!File.Exists(fileName))
- {
- File.Create(fileName);
- }
- }
- //the file would be stored at the base directory
- string fileName = AppDomain.CurrentDomain.BaseDirectory + "/app-data.txt";
- //Function for loading up the data
- public List<string> loadData()
- {
- return System.IO.File.ReadAllLines(fileName).ToList();
- }
- //adding new task on my list
- public bool addData(string model)
- {
- try
- {
- using (System.IO.StreamWriter file =
- new System.IO.StreamWriter(fileName, true))
- {
- file.WriteLine(model);
- }
- return true;
- }
- catch (Exception)
- {
- throw;
- }
- }
- //reseting the whole list
- public bool resetList()
- {
- File.WriteAllText(fileName, string.Empty);
- return true;
- }
- //complete the task or remove the task from the list
- internal bool completeTask(string selectedTask)
- {
- try
- {
- var lines = File.ReadAllLines(fileName).Where(line => line.Trim() != selectedTask.Trim()).ToArray();
- File.WriteAllLines(fileName, lines);
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- }
We now need to access this data from our application. For that, we create our class ToDoApp. It is advisable to separate the data layer and the application layer. This would create an instance of the DataPersistence class and all the functions will first perform operations on the local list then will move on to the file operations.

Join the conversation! Your thoughts help the community grow.