Perhaps this would work: Build a program that... * That has a hardcoded list of strings baked into it * Accepts as inputs a filename (but can be empty) and a substring to search for * At runtime, either (a) (if filename empty) iterates over hardcoded list of strings, or (b) opens file and iterates over lines in it * For each string / line, if contains search string then output to stdout (and maybe also increment a co…
Refactoring something along the lines of static void Main(string[] args) { var filename = args[0]; var needle = args[1]; var linesToProcess = filename != '' ? File.ReadAllLines(filename) : HardcodedLines; ProcessLines(linesToProcess, needle); } static void ProcesLines(string[] linesToProcess, string needle) { // lines-processing code... } into something like static void Main(string[] args) { var filename = args[0]; v…
If your GetLines() is returning a string[] then I agree there was no benefit. But that's not what I meant. As I said, I'm imagining the base class (ILinesProvider) to have an iterator-like interface. So, rather than string[] GetLines() method, it would have a string getNextLine() method that you call in a loop. That way, with the file, you don't load the whole thing in memory (as I also said).
If your GetLines() method is returning an IEnumerable (since this seems to be C#) then this is the problem I mentioned at the end of my comment - the base class I'm imagining is similar to an existing base class in the language, and it's confusing to write your own similar-but-slightly-different version in an example. But I think it's best to do it anyway and explain it away in a footnote. (But my ILinesProvider wouldn't return an IEnumerable, like in your snippet - instead, ILinesProvider actually is the iterator (but with a slightly different interface).)