Skip to main content
added 297 characters in body
Source Link
Adriano Repetti
  • 10.6k
  • 1
  • 24
  • 48

You can create a generator to read from standard input (see very last paragraph for a discussion about this):

It works only if console input is redirected, when input is line buffered then it'll end immediately after first line. Nice and short but simply it doesn't work. If you really really love LINQ and you can't live without one-line functions then you may do this:

static IEnumerable<string> ReadAllInputs() {
    return Enumerable.Range(0, Int32.MaxValue)
        .Select(_ => Console.ReadLine())
        .TakeWhile(x => x != null);
}

You can create a generator to read from standard input:

It works only if console input is redirected, when input is line buffered then it'll end immediately after first line. Nice and short but simply it doesn't work.

You can create a generator to read from standard input (see very last paragraph for a discussion about this):

It works only if console input is redirected, when input is line buffered then it'll end immediately after first line. Nice and short but simply it doesn't work. If you really really love LINQ and you can't live without one-line functions then you may do this:

static IEnumerable<string> ReadAllInputs() {
    return Enumerable.Range(0, Int32.MaxValue)
        .Select(_ => Console.ReadLine())
        .TakeWhile(x => x != null);
}
added 951 characters in body
Source Link
Adriano Repetti
  • 10.6k
  • 1
  • 24
  • 48

Comments. Few notes collected from comments.

Sometimes you see this kind of code:

while (Console.In.Peek() != -1)
   yield return Console.ReadLine();

It works only if console input is redirected, when input is line buffered then it'll end immediately after first line. Nice and short but simply it doesn't work.

Can we make our reader more "generic"? The obvious idea is to use it with files. Maybe yes, maybe not. First of all a console application can have piped input (from file or from another application) then we may not need this feature. If we do then we should introduce a separate class InputReader, implementation will be different for ConsoleInputReader, FileInputReader and HttpInputReader (for example); one function with a TextReader parameter will probably be the worst compromise for all our use-cases. What I'm suggesting is just to don't introduce complexity until you need it...


Comments. Few notes collected from comments.

Sometimes you see this kind of code:

while (Console.In.Peek() != -1)
   yield return Console.ReadLine();

It works only if console input is redirected, when input is line buffered then it'll end immediately after first line. Nice and short but simply it doesn't work.

Can we make our reader more "generic"? The obvious idea is to use it with files. Maybe yes, maybe not. First of all a console application can have piped input (from file or from another application) then we may not need this feature. If we do then we should introduce a separate class InputReader, implementation will be different for ConsoleInputReader, FileInputReader and HttpInputReader (for example); one function with a TextReader parameter will probably be the worst compromise for all our use-cases. What I'm suggesting is just to don't introduce complexity until you need it...

added 437 characters in body
Source Link
Adriano Repetti
  • 10.6k
  • 1
  • 24
  • 48

One final note about word counting. It may apply or not to your case but don't forget that word is a concept with different meanings in different languages. Topic is vast but try to paste a - for example - Japanese text in Microsoft Word and check what the word count is: number of characters (another vague concept). If you have to deal with international text you may need to write specific code for different cultures.


One final note about word counting. It may apply or not to your case but don't forget that word is a concept with different meanings in different languages. Topic is vast but try to paste a - for example - Japanese text in Microsoft Word and check what the word count is: number of characters (another vague concept). If you have to deal with international text you may need to write specific code for different cultures.

added 597 characters in body
Source Link
Adriano Repetti
  • 10.6k
  • 1
  • 24
  • 48
Loading
Source Link
Adriano Repetti
  • 10.6k
  • 1
  • 24
  • 48
Loading