Avatar ·

Multiline string literals in C#: how to declare a string with line breaks?

Hello everyone! I'm a beginner C# developer, switched from Python, and I've encountered a problem that seems very inconvenient to me.

\n

In Python, I could easily create multiline strings using triple quotes:

\n
text = """This is a multiline string.\nIt can contain line breaks.\nAnd it's very convenient."""
\n

But in C#, I can't find a direct analog. When I try to write a long SQL query, JSON, or just text with line breaks, I get compilation errors or have to use workarounds.

\n

Here's an example of what I want to do:

\n
string query = "SELECT * \nFROM Users \nWHERE Age > 18";
\n

Obviously, you can't write it like that — the compiler complains. I know about string concatenation and @-strings (verbatim strings), but they don't solve the problem.