Search code examples
c#network-programmingtcp

StreamReader custom newline chars


I don't understand the idea of distinction between \r\n and \r after reading the reference source code. Could anyone explain it to me? How does it works when networkstream got only \r? Which property of networkstream it checks?


Solution

  • The \r and the \n originate from the old typewriter. If you "wrote" a \r the caret would go to the beginning of the sentence. If you started typing you would type over your sentence, so to mitigate this issue they introduced the \n which would turn the caret one line down. So using \r\n actually means, set the caret to position 0, and move one line down.

    Now this has been copied to computers, with roughly the same functionality. In modern operating systems, the \n usually also resets your caret to 0 and the next line. So in essence, it actually behaves like a \r\n

    More information about this can be found on Wikipedia https://en.wikipedia.org/wiki/Newline