I already have a solution for this problem, but it seems very confusing and unpractical to me.
What I intend to do is to read a file line-by-line (due to the file size, it's unpractical to load it into memory all at once), and if certain condition is met (e.g.: the line matches a regex pattern, or contains certain keywords, or is equal to certain string)
Here's what I'd Ideally have:
void TryGetLineIf(string filePath, Condition condition, out string desiredLine)
{
StreamReader fileReader = new StreamReader(filePath);
string currentLine = "";
while (!fileReader.EndOfStream)
{
currentLine = fileReader.ReadLine();
if (condition)
{
desiredLine = currentLine;
}
}
}
However, I don't know what to do with the condition parameter. The only way out I can think of is to replace the condition with an enum, (LineSelectionOptions.IsRegexMatch, LineSelectionOptions.ContainsString ...), add an extra parameter to the void and switch between possible values for it. I'm working with .NET 2.0, if that is relevant.