This project is read-only.

Add single quote as string delimiter to allow unquoted double quotes

Topics: C# Language Design
Oct 27, 2014 at 8:14 PM
Edited Oct 27, 2014 at 8:15 PM
There are many times you wish to include text with double quotes in the string. This becomes rather infuriating regardless of whether you use "" or @"".

I propose adopting javascript's model of allowing either " or ' to allow bypassing escaping the other.

The biggest benefit to this would be in cases where you wish to declare a variable that is html.
Oct 27, 2014 at 8:34 PM
Maybe take a page from Cpp11 raw strings and include the capability to include a custom delimiter? I'd probably modify that a bit, though, as, in my opinion, the syntax of the delimiter being within the string seems a little confusing.
// The delimiter is specified between the @ and the first ", in this case the delimiter is "raw"
// The final " must be immediately followed by a " and the delimiter, all other " characters are treated as a part of the literal
var sql = @raw"SELECT "id", "field" FROM "table1" WHERE "name" = 'FOO';"raw;
// same as
var sql = @"SELECT ""id"", ""field"" FROM ""table1"" WHERE ""name"" = 'FOO';"
Built-in support in Visual Studio to copy&paste to/from normal or verbatim C# strings would be useful also. Can edit SQL/HTML/XML/whatever in a more appropriate editor and then copy&paste into C#.
Oct 27, 2014 at 9:04 PM
Edited Oct 27, 2014 at 9:05 PM
Halo_Four wrote:
Maybe take a page from Cpp11 raw strings and include the capability to include a custom delimiter? I'd probably modify that a bit, though, as, in my opinion, the syntax of the delimiter being within the string seems a little confusing.
// The delimiter is specified between the @ and the first ", in this case the delimiter is "raw"
// The final " must be immediately followed by a " and the delimiter, all other " characters are treated as a part of the literal
var sql = @raw"SELECT "id", "field" FROM "table1" WHERE "name" = 'FOO';"raw;
// same as
var sql = @"SELECT ""id"", ""field"" FROM ""table1"" WHERE ""name"" = 'FOO';"
Built-in support in Visual Studio to copy&paste to/from normal or verbatim C# strings would be useful also. Can edit SQL/HTML/XML/whatever in a more appropriate editor and then copy&paste into C#.
That's a really interesting solution. I wonder if there would be a way to do it without relying on those leading and trailing double quotes such that you could have a string like
var sql = @|SELECT "id", "field" FROM "table1" WHERE "name" = 'FOO' |;
Seeing as both quotes and double quotes are used in this, it would be great to be able to use neither single or double quotes. I don't know how the parsing would work right on this, maybe it would take a double @@ or something.
Oct 27, 2014 at 9:11 PM
I think that it's impossible, because ' reserved in C# specification for char type.
Oct 27, 2014 at 10:10 PM
dotnetchris wrote:
Seeing as both quotes and double quotes are used in this, it would be great to be able to use neither single or double quotes. I don't know how the parsing would work right on this, maybe it would take a double @@ or something.
The idea with the "custom delimiters" is that if one picks any sequence of characters which doesn't appear within the string, then all quote marks within the string will be ignored until one is found which is followed by the same tag as the start marker. Consequently, there's no need to quote any characters or worry about single quotes, double quotes, etc. All that's necessary is to pick a tag that doesn't appear in the string.