Section10.1:InterpolatedVerbatimStrings
VerbatimstringscanbecombinedwiththenewStringinterpolationfeaturesfoundinC#6.
Console.WriteLine($@”Testing\n12{5-2}
Newline”);
Output:
Testing\n123 New line
LiveDemoon.NETFiddle
Asexpectedfromaverbatimstring,thebackslashesareignoredasescapecharacters.Andasexpectedfroman interpolated string, any expression inside curly braces is evaluated before being inserted into the string at that position.
Section10.2:EscapingDoubleQuotes
DoubleQuotesinsideverbatimstringscanbeescapedbyusing2sequentialdoublequotes””torepresentone double quote “in the resulting string.
varstr=@”””Idon’tthinkso,””hesaid.”; Console.WriteLine(str);
Output:
“Idon’tthinkso,”hesaid.
LiveDemoon.NETFiddle
Section10.3:Verbatimstringsinstructthecompilertonotuse character escapes
In a normal string, the backslash character is the escape character, which instructs the compiler to look at the next character(s) to determine the actual character in the string. (Full list of character escapes)
In verbatim strings, there are no character escapes (except for “”which is turned into a “). To use a verbatim string, just prepend a @before the starting quotes.
Thisverbatimstring
varfilename=@”c:\temp\newfile.txt”
Output:
c:\temp\newfile.txt
As opposed tousing anordinary (non-verbatim) string:
varfilename=”c:\temp\newfile.txt”
thatwilloutput:
c: emp
ewfile.txt
usingcharacterescaping.(The\tisreplacedwithatabcharacterandthe\nisreplacewithanewline.) Live Demo on .NET Fiddle
varmultiLine=@”Thisisa
multilineparagraph”;
Output:
Thisisa
multilineparagraph
LiveDemoon.NETFiddle
Multi-linestringsthatcontaindouble-quotescanalsobeescapedjustastheywereonasingleline,becausethey are verbatim strings.
varmultilineWithDoubleQuotes=@”Iwenttoacitynamed
“”SanDiego””
duringsummervacation.”;
LiveDemoon.NETFiddle
Itshouldbenotedthatthespaces/tabulationsatthestartoflines2and3hereareactuallypresentinthevalueofthe variable; check this question for possible solutions.
About us and this blog
We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.
Request a free quote
We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.
Subscribe to our newsletter!
More from our blog
See all postsRecent Posts
- Chapter120:YieldKeyword 20/05/2024
- Chapter119:LockStatement 19/05/2024
- Chapter118:Makingavariablethreadsafe 18/05/2024