C# triple double quotes (three double quotes) - c#

What is the purpose of triple douple quotes """ in C#? It seems it is used for multiline text. But why not use single double quotes with #"..."?
string text = """
some text
some text
some text
""";

I think a simple example can explain better than many a text.
Suppose we have a sql query which we want to keep in a good format
to be easy readable.
If we put it naive, it won't compile:
string sql =
"select id,
name
from MyTable"; // <- Doesn't compile
We can use # to have verbatim strings which now compiles
string sql =
#"select id,
name
from MyTable";
...
// A little bit different format somewhere else in c# code
string sameSql = #"select id,
name
from MyTable";
But yet another problem arises: we have different strings and that's why
RDBMS will treat them as different queries, both versions will be parsed
and optimized, put into cache etc. So we have the same job done several times (and even worse: parsed queries cache can be flooded by same query in different formats and it'll be not enough space for other queries).
From sql we have
select id,
name
from MyTable
From sameSql we have the same query but in a different format:
select id,
name
from MyTable
Note that leading spaces are preserved (we used verbatim strings, right?) and that's a problem.
The solution is to use new """ construction
string sql =
"""
select id,
name
from MyTable
""";
...
// A little bit different format
string sameSql = """
select id,
name
from MyTable
""";
In both cases we'll get the same text
select id,
name
from MyTable
the query will be parsed, optimized and put into cache just once, c# code style ignored.

Source: C# 11 Preview Updates – Raw string literals
If you work with strings literal that contain quotes or embedded
language strings like JSON, XML, HTML, SQL, Regex and others, raw
literal strings may be your favorite feature of C# 11. Previously if
you copied a literal string with quotes into a C# literal, the string
ended at the first double quote with compiler errors until you escaped
each one. Similarly, if you copied text with curly braces into an
interpolated string literal, each curly bracket was interpreted as the
beginning of a nested code expression unless you escape it, generally
by doubling the curly bracket.
Raw string literals have no escaping. For example, a backslash is
output as a backslash, and \t is output as the backslash and a t,
not as the tab character.
Raw string literals start and end with at least three double quotes
("""..."""). Within these double quotes, single " are considered
content and included in the string. Any number of double quotes less
than the number that opened the raw string literal are treated as
content. So, in the common case of three double quotes opening the raw
string literals, two double quotes appearing together would just be
content. If you need to output a sequence of three or more double
quotes, just open and close the raw string literal with at least one
more quote than that sequence.
Raw string literals can be interpolated by preceding them with a $.
The number of $ that prefixes the string is the number of curly
brackets that are required to indicate a nested code expression. This
means that a $ behaves like the existing string interpolation – a
single set of curly brackets indicate nested code. If a raw string
literal is prefixed with $$, a single curly bracket is treated as
content and it takes two curly brackets to indicate nested code. Just
like with quotes, you can add more $ to allow more curly brackets to
be treated as content. For example:
const int veryCold = -30;
const int comfortable = 20;
string jsonString =
$$"""
{
"TemperatureRanges": {
"Cold": {
"High": {{comfortable}},
"Low": {{veryCold}}
}
}
}
""";
Raw string literals also have new behavior around automatically
determining indentation of the content based on leading whitespace. To
learn more about this and to see more examples on this feature, check
out the docs article Raw String Literals.
P.S. Thanks to Roe and ProgrammingLlama for pointing to this articles.

Related

Is there C# raw string with delimiter like in C++?

In C++, we have raw literal string using R"()" with delimiter. So this code below is fine:
const char SOMETEXT[] = R"+-+-+(<link rel="icon" href="img/favicon.png">)+-+-+"
I don't know how to do it in C#, as far as i know there is a verbatim string using #"" but it doesn't have delimiter. And this causes error:
string SOMETEXT = #"<link rel="icon" href="img/favicon.png">";
Is there any raw literal string with delimiter in C#? Because i don't want to change the string, it will PITA to edit later.
No, there's nothing like this in C#. If this is arbitrary text that you'll need to edit reasonably frequently, you might want to put it into a resource file instead.
Another alternative I often use for JSON that appears in tests etc is to just use single quotes instead of double quotes, then replace afterwards:
string text = "<link rel='icon' href='img/favicon.png'>".Replace('\'', '"');
In an #ed string (aka a verbatim string literal) you can use "" for double qoutes.
string SOMETEXT = #"<link rel=""icon"" href=""img/favicon.png"">";
In a regular string literal you can use a backslash.
string someText2 = "<link rel=\"icon\" href=\"img/favicon.png\">";
More on differences between verbatin and standard string literals
Please note that only the quote escape sequence ("") in not interpreted literally in a verbatim literal string; all others are taken literally.
Here is an example from # (C# Reference).
The following example illustrates the effect of defining a regular string literal and a verbatim string literal that contain identical character sequences.
string s1 = "He said, \"This is the last \u0063hance\x0021\"";
string s2 = #"He said, ""This is the last \u0063hance\x0021""";
Console.WriteLine(s1);
Console.WriteLine(s2);
// The example displays the following output:
// He said, "This is the last chance!"
// He said, "This is the last \u0063hance\x0021"
I don't want to change the string!
If you don't want to change the string you can embed it as a file. For unit testing I often store json files as embedded resources and load them with GetManifestResourceStream.

Is there a way to alternate single and double quotes in C# like you can in javascript?

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.
use backslash escape character
var s = "He said \"Hello\" to me";
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.
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.
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).
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.
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.

How to express a string literal containing double quotes without escape characters?

Is there a C# syntax with which I can express strings containing double quotes without having to escape them? I frequently copy and paste strings between C# source code to other apps, and it's frustrating to keep adding and removing backslashes.
Eg. presently for the following string (simple example)
"No," he said.
I write in C# "\"No,\" he said."
But I'd rather write something like Python '"No," he said.', or Ruby %q{"No," he said.}, so I can copy and paste the contents verbatim to other apps.
I frequently copy and paste strings between C# source code to other apps, and it's frustrating to keep adding and removing backslashes.
Then it sounds like you probably shouldn't have the strings within source code.
Instead, create text files which are embedded in your assembly, and load them dynamically... or create resource files so you can look up strings by key.
There's no form of string literal in C# which would allow you to express a double-quote as just a single double-quote character in source code.
You could try this but you're still effectively escaping:
string s = #"""No,"" he said.";
Update 2022: C# 11 in Visual Studio 2022 version 17.2 (or later) supports raw string literals between """ https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals
Raw string literals are a new format for string literals. Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences. A raw string literal starts with at least three double-quote (""") characters. It ends with the same number of double-quote characters. Typically, a raw string literal uses three double quotes on a single line to start the string, and three double quotes on a separate line to end the string. The newlines following the opening quote and preceding the closing quote aren't included in the final content:
Example (note that StackOverflow doesn't yet highlight correctly)
string longMessage = """
This is a long message.
It has several lines.
Some are indented
more than others.
Some should start at the first column.
Some have "quoted text" in them.
""";

What's the meaning of "#" in C#

new learner's quick question,
what's the meaning of "#" in C# codes?
Examples:
ClientDataSource.Where = #"it.ClientID==1";
cont.Notes = #"";
Response.Redirect(#"~/Default.aspx");
Thanks!
That is a verbatim string literal.
MSDN describes it as such:
Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string.
# can also be used to create identifiers that match reserved words: 2.4.2 Identifiers
For example:
var class = "Reading"; // compiler error
var #class = "Math"; // works
#"...." denotes a verbatim string literal. C# does not process any escape characters in the string, except for "" (to allow including of the " character in the string).
This makes it easier and cleaner to handle strings that would otherwise need to have a bunch of escapes to deal with properly. File/folders paths, for example.
string filePathRegular = "C:\\Windows\\foo\\bar.txt";
string filePathVerbatim = #"C:\Windows\foo\bar.txt";
It's also very useful in writing Regular Expressions, and probably many other things.
It's worth noting that C# also uses the # character as a prefix to allow reserved words to be used as identifiers. For example, Html Helpers in ASP.Net MVC can take an anonymous object containing HTML attributes for the tags they create. So you might see code like this:
<%= Html.LabelFor(m => m.Foo, new { #class = "some-css-class" } ) %>
The # is needed here because class is otherwise a reserved word.
The verbatim string literal allows you to put text inside of a string that would otherwise be treated differently by the compiler. For example, if I were going to write a file path and assign it to a variable, I might do something like so:
myString = "C:\\Temp\\Test.txt";
The reason I have to have the double slashes is because I am escaping out the slash so it isn't treated as an command. If I use the verbatim string literal symbol, my code could look as follows:
myString = #"C:\Temp\Test.txt";
It makes it easier to write strings when you are dealing with special characters.

something like a python's triple-quote in F# (or C#)?

I want to assign a xml code into a string variable.
I can do this without escaping single or double-quotes by using triple-quote in python.
Is there a similar way to do this in F# or C#?
F# 3.0 supports triple quoted strings. See Visual Studio F# Team Blog Post on 3.0 features.
The F# 3.0 Spec Strings and Characters section specifically mentions the XML scenario:
A triple-quoted string is specified by using three quotation marks
(""") to ensure that a string that includes one or more escaped
strings is interpreted verbatim. For example, a triple-quoted string
can be used to embed XML blobs:
As far as I know, there is no syntax corresponding to this in C# / F#. If you use #"str" then you have to replace quote with two quotes and if you just use "str" then you need to add backslash.
In any case, there is some encoding of ":
var str = #"allows
multiline, but still need to encode "" as two chars";
var str = "need to use backslahs \" here";
However, the best thing to do when you need to embed large strings (such as XML data) into your application is probably to use .NET resources (or store the data somewhere else, depending on your application). Embedding large string literals in program is generally not very recommended. Also, there used to be a plugin for pasting XML as a tree that constructs XElement objects for C#, but I'm not sure whether it still exists.
Although, I would personally vote to add """ as known from Python to F# - it is very useful, especially for interactive scripting.
In case someone ran into this question when looking for triple quote strings in C# (rather than F#), C#11 now has raw string literals and they're (IMO) better than Python's (due to how indentation is handled)!
Raw string literals are a new format for string literals. Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences. A raw string literal starts with at least three double-quote (""") characters. It ends with the same number of double-quote characters. Typically, a raw string literal uses three double quotes on a single line to start the string, and three double quotes on a separate line to end the string. The newlines following the opening quote and preceding the closing quote are not included in the final content:
string longMessage = """
This is a long message.
It has several lines.
Some are indented
more than others.
Some should start at the first column.
Some have "quoted text" in them.
""";
Any whitespace to the left of the closing double quotes will be removed from the string literal. Raw string literals can be combined with string interpolation to include braces in the output text. Multiple $ characters denote how many consecutive braces start and end the interpolation:
var location = $$"""
You are at {{{Longitude}}, {{Latitude}}}
""";
The preceding example specifies that two braces starts and end an interpolation. The third repeated opening and closing brace are included in the output string.
https://devblogs.microsoft.com/dotnet/csharp-11-preview-updates/#raw-string-literals
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11
As shoosh said, you want to use the verbatim string literals in C#, where the string starts with # and is enclosed in double quotation marks. The only exception is if you need to put a double quotation mark in the string, in which case you need to double it
System.Console.WriteLine(#"Hello ""big"" world");
would output
Hello "big" world
http://msdn.microsoft.com/en-us/library/362314fe.aspx
In C# the syntax is #"some string"
see here

Categories

Resources