Take the 2-minute tour ×

I'm starting to learn C#, and I don't understand why regular string literals (i.e., " ") cannot contain literal newline characters. (I'm not talking about the escape sequence \n). I know that you must use verbatim string literals (i.e., @" ") for multiline strings, but why?

regular string produces "Newline in constant" error verbatim string produces no error

I've not seen it explicitly stated that you cannot use them in regular strings. More than that, except where it's mentioned in passing that I can use verbatim strings for this, everything I've read seems to suggest that literal newline characters would be allowed in regular string literals.

Beginning Visual C# 2010 and Code: Generating Multiline String Literals (Visual C#) show examples of verbatim multiline strings with no further explanation.

Learning C# 3.0 says this:

In the C# language, spaces, tabs, and newlines are considered to be whitespace.... Extra whitespace is generally ignored in C# statements. ... The exception to this rule is that whitespace within a string is treated as literal; it is not ignored.

So it's literal? That's what I would expect too, but it's not.
It even includes this tip box:

Visual Basic programmers take note: in C#, the end-of-line has no special significance. Statements are ended with semicolons, not newline characters. There is no line continuation character because none is needed.

(I realize that this is talking about outside of strings, but why would end-of-line have special parsing significance inside a string if it doesn't outside a string?)

Having finally found my way to the string (C# Reference) itself, I still garnered no insight:

String literals can contain any character literal. Escape sequences are included. The following example uses escape sequence \\ for backslash, \u0066 for the letter f, and \n for newline.

It says that escape sequences can be used, but it does not say they must be used. Are literal newline characters not included in "any character literal"? If I have a string that contains a literal tab character instead of its escape sequence \t, there is no error. But if I have a literal newline, I get an error. I've even changed the file's line endings from \r\n to \n or \r to no effect.

Obviously, I'm able to infer from examples and from Visual Studio errors that a verbatim string is required if it contains a literal newline character, but everything I've read suggests that shouldn't be the case. Why the difference?

share|improve this question
I think it's probably one of those "because that's how the language works" things.. It was probably inspired from other languages that new C# programmers would be coming from. As you mentioned, you can use the @ prefix to define a verbatim string constant. –  Mike Christensen Aug 30 '12 at 22:00
@MikeChristensen Oh yeah, and I tried escaping the literal newline with a backslash like I've seen in C/C++. No luck there, either. –  Wiseguy Aug 30 '12 at 22:04

3 Answers 3

up vote 3 down vote accepted

Well, shoot. Right as I was submitting this, I found the answer.


Apparently, no, they aren't. Character literals:


' character '




Any character except ' (U+0027), \ (U+005C), and new-line-character

share|improve this answer
Yeah. But why aren't new-line characters included in character literals? –  Zaid Masud Aug 30 '12 at 22:08
@ZaidMasud Language design choices are well above the scope of my question. My objection was simply that this characteristic was not clearly defined, which, in fact, it apparently is. –  Wiseguy Aug 30 '12 at 22:15
Yes the specification is consistent. More of a curious musing on my part. –  Zaid Masud Aug 30 '12 at 22:22

Likely dupe of Why must C/C++ string literal declarations be single-line?

In a nutshell, because the C language doesn't support it.

A typo that leaves a string literal unclosed would slurp the rest of the file as a single token, leaving the programmer with a compiler error message along the lines of "expecting a semi-colon at line xxx, column yyy" where the indicated location is the end of the source file.

Mostly you don't use multi-line literals. Better to make them explicit from a UX perspective.

Further, in the constrained environment the C language was developed in (8K PDP-11?), I suspect that sort of overflow might crash the compiler.

The C language does support literal splicing, though, which is helpful:

char *txt = "this is line 1\n"
            "this is line 2\n"
            "this is line 3\n"

It also supports line splicing:

char *txt = "this is my\n\
 multi-line string literal\n\
 isn't it nice?\n" ;

Features that I wish C# had.

share|improve this answer
Yeah, I tried splicing too, to no avail. Was a little surprised by that since, if the limitation is inherited from C-family ancestors, I figured it would inherit that as well. –  Wiseguy Aug 30 '12 at 22:13
Yeah, splicing sort of feels like it should be there. The fact one can concatenate with + was perhaps seen as removing the need. –  Jon Hanna Aug 30 '12 at 22:18

C# (along with C++, C, Java, which influenced its syntax) have a very simple rule for whitespace:

You can do what you want with it.

This lets use format things however you want for the benefit of readability. Now, a Python fan might say that advantage is overrated, but it is an advantage, that we do make use of.

Newlines in strings could mess that up. All the moreso if you aren't sure whether the newline in the source should mean that we insert "\u000D", "\u000A", "\u000A\u000D", "\u0085", "\u000B", "\u000C", "\u2028" or "\u2029" into the string, all of which have newline semantics and the first four of which have been different system's "only sane way of doing newline, everyone else is wrong".

You could still argue that the downside of allowing it is overrated. C# does - after all, the form of strings that are not as people might expect from C++ etc. does allow it.

share|improve this answer
I figured "what does it matter?" so long as the string is eventually terminated with a closing quote (as is the case with PHP, for example), but good point about the ambiguity of the line ending. –  Wiseguy Aug 30 '12 at 23:29

Your Answer


