-2

In javascript I can write

    var s = 'He said "Hello" to me';
    or 
    var s = "don't be sad";

In other words, if I want a double quote in the string, I can declare the string using single quotes. If I want a single quote in the string, I can declare the string using double quotes. This is super handy. Has anyone found a way to do something similar in C#?

The single quotes are easy, c# already works with embedding single quotes. As for double quotes, there seems to be 2 common options.

  1. use backslash escape character

    var s = "He said \"Hello\" to me";
    
  2. use single quotes and replace them with double quotes

    var s = "He said 'Hello' to me";
    s = s.Replace("'","\"");
    

I can live with option 2, but better is better.

rocketsarefast
  • 4,072
  • 1
  • 24
  • 18
  • 4
    No, all strings in C# use double quotes. It's part of the language spec. –  Jan 31 '19 at 15:35
  • See https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/index - single quotes denote *chars*, so you need to escape double quotes in string literals. – jonrsharpe Jan 31 '19 at 15:36
  • `var s = @"He said ""Hello"" to me"` is the closest thing you get to convenience that doesn't involve ``\``. If you want a flippant answer, then technically `var s = $"He said {'"'}Hello{'"'} to me"` uses single quotes to get a double quote... – Jeroen Mostert Jan 31 '19 at 15:37
  • I'm confused - why not try it and find out if it compiles rather than posting it here? (Side note: it doesn't). – EJoshuaS - Stand with Ukraine Jan 31 '19 at 15:39
  • 1
    C# having a character type while JavaScript has only strings is the major reason why this syntax can't work, incidentally. In C#, `'\u005c'` is one single character, which is of a distinct type than a single-character string. Introducing exceptions where constants are sometimes parsed as strings and sometimes parsed as characters based on what's in them would not simplify things, although the value of having character constants at all is questionable -- but that's a holdover from C. – Jeroen Mostert Jan 31 '19 at 15:45

4 Answers4

5

Javascript and C# are both based on C.

In C ' is used to delimit characters and " is used to delimit strings.

In Javascript there is no character type, so if you want a character you use a single-character string. As such it only needs " but many people used to C-style languages were used to using ' and preferred that for such single-character strings used for characters. This soon led to people developing other styles, especially since the escaping rules would make it convenient in exactly the way you say.

C# does have a character type, as well as a string type, so it kept to the C syntax in this regard.

The feature of Javascript syntax arose due to a feature of its typing system that isn't matched by C#, along with their shared history of borrowing syntax from C. It wouldn't work in C# if the designers had wanted it, without massively complicating the distinction between string and char literals.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
1

No, there is no way.

Single quotes ' are char variable delimiter

Double quotes " are string variable delimiter

Check the MS string programing guide for further information.

Cleptus
  • 3,446
  • 4
  • 28
  • 34
1

You can define your extension method on string to little speedup your work and reduce chance to misstype something.

    public static string ApostrophesToQuotes(this string s)
    {
        return s.Replace('\'', '"');
    }

And there is one more way to write quotes in string literal.

var s = @"he said ""Hello"" to me");

But you can't mix them, because apostrophes are for single character literal (2 byte integer in UTF-16) and quotes are for string literals (array of characters).

  • I like the function idea. It could keep the whole thing on one line. As for the double double quotes, I find it really hard to read when the text gets complex. – rocketsarefast Jan 31 '19 at 19:29
0

I decided to just use single quotes and replace them with double quotes.

    var s = "He said 'Hello' to me";
    s = s.Replace("'","\"");

It is slightly annoying because I need to paste things like this

    {"children":[{"domid":"sbsmStatusFilter","datatitle":"SBS Status","dataicon":"img/appicons/rsm4-64x64.png"},{"domid":"sbsMonitoringFilters","datatitle":"Monitoring","dataicon":"img/appicons/monitoring64x64.png"}]}

and then use the text editor find replace function to replace all the double quotes with single quotes, then add the text replace code, and I eventually turn it into this

    var text3 = @"{'children':[{'domid':'sbsmStatusFilter','datatitle':'SBS Status','dataicon':'img/appicons/rsm4-64x64.png'},{'domid':'sbsMonitoringFilters','datatitle':'Monitoring','dataicon':'img/appicons/monitoring64x64.png'}]}";
    text3 = text3.Replace("'", "\"");

Not too bad I guess. At least the text is fairly easy to read so people can modify it.

rocketsarefast
  • 4,072
  • 1
  • 24
  • 18