-2

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?

CᴴᴀZ
  • 521
  • 7
  • 20
  • The first and last quotes are the delimiters. For example `String s = @"User ""ChaZ"" asks about quotes";` – Ed Gibbs Dec 30 '14 at 13:02
  • The compiler still needs to know where the string ends. – Rowland Shaw Dec 30 '14 at 13:03
  • Because your computer (and no other, for that matter) doesn't know where you want your string to stop? Not interpreting escape sequences is one thing, but that one character is still special... – Janis F Dec 30 '14 at 13:03
  • Is this a question or a rant? Also, you seem to ask about a C# language design decision while you don't really understand the basic syntax. – Kobi Dec 30 '14 at 13:06
  • @RowlandShaw @ed @kobi @rominox: It can know where the string ends by using predecessor of the inner quote. E.g.: `@"1"2"";` The quote preceding `1` can be used as start point and quote preceding `;` can be the end point, whatever in the between is the required string. Why an extra pair? – CᴴᴀZ Dec 30 '14 at 13:15
  • @ChaZ What about `someMethod(@"abc","123");`, Are we passing one string with value `abc","123` or two strings with values `abc` and `123`? – juharr Dec 30 '14 at 14:23
  • @juharr: Great counter argument! Thanks, now I very well understand why double quotations are necessary in verbatim strings. – CᴴᴀZ Dec 30 '14 at 14:33
  • Hello! I edited the question and hope it follows the requisite norms. Kindly reopen as the fruitful answers might serve the purpose for future-comers. – CᴴᴀZ Jan 04 '15 at 15:57

2 Answers2

7

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.

Kobi
  • 135,331
  • 41
  • 252
  • 292
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • :Thanks for answering but the confusion still prevails. The parser can understand the start and end point of the string by utilizing the _first_ occurring `"` and last occurring `"` (before `;`). For e.g.: `@"1"2"";` The quote preceding `1` can be used as start point and quote preceding `;` can be the end point, the content in between these two points should be parsed as string. Why an extra pair is needed by the compiler to parse it effectively? Pardon for my queries, but I'm trying to learn C# from basics as am a student. – CᴴᴀZ Dec 30 '14 at 13:26
  • 3
    @Chaz: No, that's not how the parser works - and it can't be, as there could be "x" + "y" for example, where the end of the first literal is *not* the last quote before a semicolon. The parser works by consuming tokens as it goes - it needs to know whether it's 'in' a string literal or not. Take your example but change the 2 to a +. Is that now a single verbatim string literal or a verbatim string literal concatenated with a regular string literal in your hypothetical language? – Jon Skeet Dec 30 '14 at 13:30
  • jon: `The parser works by consuming tokens as it goes` - Oh! How can I forget this from my Compiler Design classes? Thanks a lot Sir Skeet as your counter question made it clear for me. And am really happy to get a reply from you as I always used to wonder about you (got downvotes for the question though), but that's the trade-off for something better. :) – CᴴᴀZ Dec 30 '14 at 13:38
  • 1
    @ChaZ: I've edited my answer to go into more details about the disadvantages of your suggestion for how you thought the language should work. – Jon Skeet Dec 30 '14 at 13:42
2

That's because you need to have an opening and a closing " characters too. So with a line like yours:

s=@"""Hello""";

The first and the last quote characters is to indicate the start and the end of the string to the compiler. And then within the string the double quotation sequence ("") is to indicate you wish to have a quote character in your string.

msporek
  • 1,187
  • 8
  • 21