Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heredoc strings in C#

Tags:

string

c#

heredoc

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

like image 833
Aillyn Avatar asked Aug 21 '10 18:08

Aillyn


People also ask

What is heredoc C?

Here document (Heredoc) is an input or file stream literal that is treated as a special block of code. This block of code will be passed to a command for processing. Heredoc originates in UNIX shells and can be found in popular Linux shells like sh, tcsh, ksh, bash, zsh, csh.

What is heredoc give example?

Heredoc's are equivalent to a double quoted string. That means any variables in the string will get substitued for their respective values. We could rewrite the double quoted string example above as a heredoc:- $foo = 'bar'; echo <<<EOT Hello $foo Goodbye! EOT; // Output:- // Hello bar // Goodbye!

What is heredoc used for?

Heredoc is one of the ways to store or print a block of text in PHP. The data stored in the heredoc variable is more readable and error-free than other variables for using indentation and newline.

What is heredoc syntax?

The heredoc syntax is a way to declare a string variable. The heredoc syntax takes at least three lines of your code and uses the special character <<< at the beginning.


1 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
like image 72
Jon Skeet Avatar answered Sep 20 '22 19:09

Jon Skeet