Heredoc strings in C#
Asked Answered
S

4

55

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)?

Swashbuckler answered 21/8, 2010 at 18:43 Comment(1)
For those who didn't know what heredoc was, like me en.wikipedia.org/wiki/Here_documentPectase
M
60

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
Montford answered 21/8, 2010 at 19:5 Comment(10)
Mono folks did look at it, though: tirania.org/blog/archive/2009/Dec-20.htmlMuscat
@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...Montford
That is a common (although not universal) feature of heredoc strings, but: yes, double edged in many casesMuscat
guess the same is true for nowdoc syntax as well?Altazimuth
@hanshenrik: I'd never heard of nowdoc until your comment - after 30 seconds of searching, that sounds like a PHP-only syntax.Montford
@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 2011Altazimuth
@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.Montford
@JonSkeet interesting, maybe interpolated string literals would be a good addition to your answer ^^Altazimuth
@hanshenrik: Not really, as the OP was really asking about not having to escape anything.Montford
@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), 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 askAltazimuth
T
51

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/

Tendril answered 16/9, 2011 at 14:14 Comment(1)
you can now do a verbatim interpolated string to avoid the String.Format part, see this other question: #31639079Walkthrough
T
17

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.
""";
Thriller answered 2/11, 2022 at 18:29 Comment(0)
P
13

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

Poly answered 21/8, 2010 at 18:52 Comment(2)
+1 "doublequotes need to be escaped by doubling" -- was what i was looking for.Thordis
@Poly This answer is outdated. Any chance to do something about it?Thriller

© 2022 - 2024 — McMap. All rights reserved.