Is there a heredoc notation for strings in C#, preferably one where I don't have to escape anything (including double quotes, which are a quirk in verbatim strings)?
4 Answers
As others have said, there isn't.
Personally I would avoid creating them in the first place though - I would use an embedded resource instead. They're pretty easy to work with, and if you have a utility method to load a named embedded resource from the calling assembly as a string (probably assuming UTF-8 encoding) it means that:
- If your embedded document is something like SQL, XSLT, HTML etc you'll get syntax highlighting because it really will be a SQL (etc) file
- You don't need to worry about any escaping
- You don't need to worry about either indenting your document or making your C# code look ugly
- You can use the file in a "normal" way if that's relevant (e.g. view it as an HTML page)
- Your data is separated from your code

- 1,421,763
- 867
- 9,128
- 9,194
-
1Mono folks did look at it, though: http://tirania.org/blog/archive/2009/Dec-20.html – Marc Gravell Apr 10 '12 at 19:46
-
@MarcGravell: That's talking about interpolation rather than not having to escape anything, isn't it? I only scanned through it quickly, but I would have thought that would mean escaping *more*, not less... – Jon Skeet Apr 10 '12 at 20:48
-
1That is a common (although not universal) feature of heredoc strings, but: yes, double edged in many cases – Marc Gravell Apr 10 '12 at 21:31
-
-
@hanshenrik: I'd never heard of nowdoc until your comment - after 30 seconds of searching, that sounds like a PHP-only syntax. – Jon Skeet Nov 08 '20 at 16:46
-
@JonSkeet actually perl has supported what the PHP project refers to as the "nowdoc" syntax since at least 2009 (possibly earlier), but im not sure the Perl project actually uses that name, or has a name for it at all, it's possible that what the Perl project calls `heredoc without interpolation` is what the PHP project calls `nowdoc` - the syntax for nowdoc is the same in both PHP and Perl though. and Perl did it first, PHP first added support in 6 January 2011 – hanshenrik Nov 08 '20 at 17:07
-
@hanshenrik: Okay, that's still not *terribly* helpful - if you're going to ask about whether a language supports something, it's really useful to describe exactly what you mean rather than assuming other people know the term (from a different language) already. But the answer is still no - C# now has interpolated string literals, but that's the only change in terms of string literals since 2010. – Jon Skeet Nov 08 '20 at 17:48
-
@JonSkeet interesting, maybe `interpolated string literals` would be a good addition to your answer ^^ – hanshenrik Nov 08 '20 at 20:41
-
@hanshenrik: Not really, as the OP was really asking about not having to escape anything. – Jon Skeet Nov 08 '20 at 21:01
-
@JonSkeet fair. in 2009, Perl thought `non-interpolated heredoc` was a good idea. in 2011, PHP thought the same thing. at some point Ruby also thought it was a good idea ([ref](https://www.rubyguides.com/2018/11/ruby-heredoc/)), given that multiple languages think it's a good idea, i wonder if the C# designers has an opinion on it, or perhaps nobody ever suggested it to them.. not that this comment section is the right place to ask – hanshenrik Nov 08 '20 at 22:18
Well even though it doesn't support HEREDOC's, you can still do stuff like the following using Verbatim strings:
string miniTemplate = @"
Hello ""{0}"",
Your friend {1} sent you this message:
{2}
That's all!";
string populatedTemplate = String.Format(miniTemplate, "Fred", "Jack", "HelloWorld!");
System.Console.WriteLine(populatedTemplate);
Snagged from: http://blog.luckyus.net/2009/02/03/heredoc-in-c-sharp/

- 66,836
- 64
- 257
- 336
-
1you can now do a verbatim interpolated string to avoid the String.Format part, see this other question: https://stackoverflow.com/questions/31638579/how-do-you-use-verbatim-strings-with-interpolation – santiago arizti Apr 16 '18 at 20:30
No, there is no "HEREDOC" style string literal in C#.
C# has only two types of string literals:
- Regular literal, with many escape sequences necessary
- Verbatim literal,
@
-quoted: doublequotes need to be escaped by doubling
References
- csharpindepth.com - General Articles - Strings
- MSDN - C# Programmer's Reference - Strings
String literals are of type
string
and can be written in two forms, quoted and@
-quoted.

- 376,812
- 128
- 561
- 623
-
5+1 "doublequotes need to be escaped by doubling" -- was what i was looking for. – blak3r Sep 26 '11 at 04:27
-
@polygenelubricants This answer is outdated. Any chance to do something about it? – Artur INTECH Feb 06 '23 at 16:12
November 2022 update:
Starting with C# 11 this is now possible using Raw string literals:
var longMessage = """
This is a long message.
Some "quoted text" here.
""";

- 6,024
- 2
- 37
- 34