Home >Backend Development >C++ >How Can I Create Multiline String Literals in C#?

How Can I Create Multiline String Literals in C#?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-23 06:42:12994browse

How Can I Create Multiline String Literals in C#?

Creating multi-line string literals in C#

Multiline string literals allow you to define strings across multiple lines. In C#, there are two ways to create multiline string literals.

1. Verbatim string literal:

You can create a verbatim string literal by adding the "@" symbol before the string literal. This allows you to include newlines, special characters, and spaces as-is.

<code class="language-c#">string query = @"SELECT foo, bar
FROM table
WHERE id = 42";</code>

2. String interpolation:

Alternatively, you can use string interpolation to create multiline strings. This method requires C# 6.0 or higher.

<code class="language-c#">string query = $"SELECT foo, bar
FROM table
WHERE id = 42";</code>

In both cases you can avoid escaping special characters except double quotes in the string.

The above is the detailed content of How Can I Create Multiline String Literals in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn