Noel Widmer
Noel Widmer

Reputation: 4572

C# grammar: What character is the "new line" character?

I am implementing my own Lexer and just had a look at how C# handles char literals: https://msdn.microsoft.com/en-us/library/aa691087(v=vs.71).aspx

The C# char grammer is described in the link above
It specifies that the "new line" character is not allowed as a character literal.
But it doesn't state what the "new line" character is.
From my perspective it could be one of the following:

  1. CR + LF (windows)
  2. LF (unix & mac)
  3. CR (mac)
  4. CR || LF || CR + LF (all of them)

Since one file could be created on f.e. mac and then be compiled on windows I tend to believe that the "new line" character matches the grammer in case 4. I remember VS asking me if I'd like to convert my line endings to match the windows line endings in some cases. Since we can decline that option it is possible to stick with whatever endings the file contains.
Can someone confirm case 4 or tell me that I'm wrong?

Upvotes: 3

Views: 181

Answers (1)

Kuba Wyrostek
Kuba Wyrostek

Reputation: 6221

Indeed, it can be either CR or LF, and a few more, according to spec:

https://msdn.microsoft.com/en-us/library/aa664812(v=vs.71).aspx

new-line-character:
    Carriage return character (U+000D)
    Line feed character (U+000A)
    Line separator character (U+2028)
    Paragraph separator character (U+2029)

Upvotes: 5

Related Questions