Search code examples
c#programming-languagesdouble-quotesverbatim-string

Why C# requires two double-quotes to print " in case of verbatim string?


If I store a verbatim string, string s=@""Hello""; it should hypothetically parse the string and print "Hello" (taking the " character as a part of the string)

but in actual practice it fails and instead string s=@"""Hello"""; displays the desired output ("Hello").

Why the syntax demands an extra " character? What would be the disruptive outcomes if C# starts functioning in the aforementioned hypothetical manner?


Solution

  • So technically, string s=@""Hello""; should print "Hello"

    No, that would just be invalid. The compiler should - and does - obey the rules of the language specification.

    Within a verbatim string literal, double-quotes must be doubled (not tripled) to distinguish them from the double-quote just meaning "the end of the string".

    It's easiest to see this if you use a double-quote within the middle of a string, not at the start or end:

    string x = @"start "" end";
    

    F# works perfectly in the above case

    Not as far as I can see. In a sample F# project in VS2015 Preview:

    let x = @""Hello""
    

    ... gives an error, whereas @"""Hello""" results in a string of "Hello".

    Additionally, the F# verbatim string literal documentation suggests it works exactly as per C#:

    If preceded by the @ symbol, the literal is a verbatim string. This means that any escape sequences are ignored, except that two quotation mark characters are interpreted as one quotation mark character.

    Basically, it sounds like verbatim string literals work in both C# and F# perfectly well, with both of them requiring double-quotes to be doubled. F# also has tripled-quoted strings, within which they don't need to be doubled... but that's just a separate feature that C# doesn't have.

    EDIT: To answer your comment about ambiguity, you gave an example of @"1"2"". Let me change that very slightly:

    string x = @"1"+"";
    

    In C# at the moment, that means the concatenation of a verbatim string literal with content 1 and an empty regular string literal. If you're proposing that it should actually be a single verbatim string literal with content 1"+", with the parser relying on the quote directly before the next semi-colon being the end of the verbatim string literal, that strikes me as a really bad idea. For example, suppose I want to use a verbatim string literal as the first argument in a two-argument method call, like this:

    Console.WriteLine(@"Foo {0}", "Bar");
    

    By your rules, that would be a call with a single argument - it would be impossible to represent the call we're trying to make, as a single statement. (You'd need to use a variable for the verbatim string literal to avoid it messing things up.)

    In short, I'm happier with the way C# already works.