The following is my version of loosely writing out a path to consider which can take different directions but I like to pre-process data for validation purposes, here is just the int aspect.
Note there is more validation like is there a value when splitting data, is the data delimited in the first place, are there blank lines etc. Yet a good reason to have validation when data is being saved to a file and a better choice for a file is json.
So let's say the data is first created using say an Employee class, all well defined properties, this would be the first step followed by assertion e.g. if id is missing which they should never be than stop right there etc.
public class FileOperations
{
private string _fileName = "TODO";
public (bool success, List<Employee> list) ReadEmployees()
{
List<Employee> list = new();
var lines = File.ReadAllLines(_fileName);
var hasValidIdentifierAsIntlines =
lines
.Select(x => x.Split(','))
.All(x => int.TryParse(x[0], out var _));
return hasValidIdentifierAsIntlines ? (true, Process(lines)) : (false, null);
}
private List<Employee> Process(string[] lines)
{
throw new NotImplementedException();
}
}
public class Employee
{
public int Id { get; set; }
}