Section16.1:Escapingspecialsymbolsinstringliterals
Backslash
//Thefilenamewillbec:\myfile.txtinbothcases
stringfilename=”c:\\myfile.txt”;
stringfilename=@”c:\myfile.txt”;
Thesecondexampleusesaverbatimstringliteral,whichdoesn’ttreatthebackslashasanescapecharacter.
Quotes
stringtext=”\”HelloWorld!\”,saidthequickbrownfox.”;
stringverbatimText=@”””HelloWorld!””,saidthequickbrownfox.”;
Bothvariableswillcontainthesame text.
“Hello World!”, said the quickbrown fox.
Newlines
Verbatim string literals can contain newlines:
stringtext=”Hello\r\nWorld!”;
stringverbatimText=@”Hello
World!”;
Bothvariableswillcontainthesame text.
Section16.2:Unicodecharacterescapesequences
stringsqrt=”\\u221A”; //√
stringemoji=”\\U0001F601″;//?
stringtext=”\\u0022HelloWorld\\u0022″;//”HelloWorld”
stringvariableWidth=”\\x22HelloWorld\\x22″;//”HelloWorld”
Section16.3:Escapingspecialsymbolsincharacterliterals
Apostrophes
charapostrophe=’\’‘;
Backslash
charoneBackslash=’\\‘;
Section16.4:Usingescapesequencesinidentifiers
Escapesequencesarenotrestrictedtostringandcharliterals.
Supposeyouneedtooverrideathird-partymethod:
protectedabstractIEnumerable<Texte>ObtenirŒuvres();
andsupposethecharacterŒisnotavailableinthecharacterencodingyouuseforyourC#sourcefiles.Youare lucky,itispermittedtouseescapesofthetype\u####or\U########inidentifiersinthecode.Soitislegalto write:
protectedoverrideIEnumerable<Texte>Obtenir\u0152uvres()
{
//…
}
andthe C# compiler will knowŒand \u0152are the same character.
(However,itmightbeagoodideatoswitchtoUTF-8orasimilarencodingthatcanhandleallcharacters.)
Section16.5:Unrecognizedescapesequencesproduce compile-time errors
Thefollowing exampleswill not compile:
strings=”\c“;
charc=’\c‘;
Instead,theywillproducetheerrorUnrecognizedescapesequenceatcompiletime.
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