Edit

Resolve errors and warnings for string literal declarations

The C# compiler generates errors and warnings when you declare string literals with incorrect syntax or use them in unsupported contexts. These diagnostics help you identify issues with basic string literals, character literals, raw string literals, and UTF-8 string literals.

  • CS1009: Unrecognized escape sequence.
  • CS1010: Newline in constant.
  • CS1011: Empty character literal.
  • CS1012: Too many characters in character literal.
  • CS1039: Unterminated string literal.
  • CS8996: Raw string literals are not allowed in preprocessor directives.
  • CS8997: Unterminated raw string literal.
  • CS8998: Not enough starting quotes for this raw string content.
  • CS8999: Line does not start with the same whitespace as the closing line of the raw string literal.
  • CS9000: Raw string literal delimiter must be on its own line.
  • CS9001: Multi-line raw string literals are only allowed in verbatim interpolated strings.
  • CS9002: Multi-line raw string literals must contain at least one line of content.
  • CS9003: Line contains different whitespace than expected.
  • CS9004: Not enough quotes for raw string literal.
  • CS9005: Not enough closing braces for interpolated raw string literal.
  • CS9006: Too many opening braces for interpolated raw string literal.
  • CS9007: Too many closing braces for interpolated raw string literal.
  • CS9008: Sequence of '@' characters is not allowed.
  • CS9009: String must start with quote character.
  • CS9026: The input string cannot be converted into the equivalent UTF-8 byte representation.
  • CS9047: Operator cannot be applied to operands that are not UTF-8 byte representations.
  • CS8103: Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals.
  • CS9274: Cannot emit this string literal into the data section because it has XXHash128 collision with another string literal.
  • CS9315: Combined length of user strings used by the program exceeds allowed limit. Adding a string literal requires restarting the application.

Incorrectly formed string literals

  • CS1009 - Unrecognized escape sequence.
  • CS1010 - Newline in constant.
  • CS1011 - Empty character literal.
  • CS1012 - Too many characters in character literal.
  • CS1039 - Unterminated string literal.

To correct these errors, apply the following techniques:

  • Use one of the standard escape sequences defined in the C# language specification, such as \n (newline), \t (tab), \\ (backslash), or \" (double quote) (CS1009). The compiler doesn't recognize escape sequences that aren't part of the language specification, so using undefined escape sequences causes this error because the compiler can't determine what character you intended to represent.
  • Add the closing quote character to complete your string literal (CS1039). String literals must have both an opening and closing delimiter, so an unterminated string causes the compiler to treat subsequent source code as part of the string content, which leads to parsing errors.
  • Add exactly one character between the single quotes in your character literal (CS1011, CS1012). Character literals represent a single character value and must contain exactly one character or a valid escape sequence, so empty character literals or those containing multiple characters violate the language rules for the char type.
  • Split string literals that span multiple source lines by ending each line with a closing quote and starting the next line with an opening quote, using the + operator to concatenate them (CS1010). Regular string literals can't contain actual newline characters because the closing quote must appear on the same line as the opening quote, but you can achieve multi-line strings through concatenation or by using verbatim strings or raw string literals, which allow embedded newlines as part of the string content.

For more information, see strings.

Incorrectly formed raw string literals

  • CS8996 - Raw string literals are not allowed in preprocessor directives.
  • CS8997 - Unterminated raw string literal.
  • CS8998 - Not enough starting quotes for this raw string content.
  • CS8999 - Line does not start with the same whitespace as the closing line of the raw string literal.
  • CS9000 - Raw string literal delimiter must be on its own line.
  • CS9001 - Multi-line raw string literals are only allowed in verbatim interpolated strings.
  • CS9002 - Multi-line raw string literals must contain at least one line of content.
  • CS9003 - Line contains different whitespace than expected.
  • CS9004 - Not enough quotes for raw string literal.
  • CS9005 - Not enough closing braces for interpolated raw string literal.
  • CS9006 - Too many opening braces for interpolated raw string literal.
  • CS9007 - Too many closing braces for interpolated raw string literal.
  • CS9008 - Sequence of '@' characters is not allowed.
  • CS9009 - String must start with quote character.

To correct these errors, apply the following techniques:

  • Use regular string literals or verbatim string literals instead of raw string literals in preprocessor directives like #if, #define, or #pragma (CS8996). Preprocessor directives are evaluated during the preprocessing phase before lexical analysis occurs, so the compiler can't recognize raw string literal syntax in these contexts because raw strings are identified during the later lexical analysis phase.
  • Add a closing delimiter that matches the opening delimiter to complete your raw string literal (CS8997, CS9004). The raw string literal syntax requires that the opening and closing delimiters contain the same number of consecutive double-quote characters (at least three), so a missing or mismatched closing delimiter prevents the compiler from determining where the string content ends.
  • Place the opening and closing delimiters of multi-line raw string literals on their own lines, with no other content on those lines (CS9000). The multi-line raw string format rules require delimiters to occupy dedicated lines to establish clear boundaries for the string content and to enable the whitespace trimming behavior that removes common leading indentation from all content lines.
  • Add at least one line of content between the opening and closing delimiters of your multi-line raw string literal (CS9002). The language specification requires multi-line raw strings to contain actual content because empty multi-line raw strings serve no purpose and likely indicate incomplete code, whereas single-line raw strings (with delimiters on the same line) can be empty and are the appropriate syntax for empty string values.
  • Adjust the indentation of your raw string content lines to match or exceed the indentation of the closing delimiter line (CS8999, CS9003). The whitespace handling rules for raw string literals use the closing delimiter's leading whitespace as the baseline for trimming common indentation from all content lines, so content lines with less indentation than the closing delimiter violate this trimming algorithm and indicate incorrect formatting.
  • Increase the number of double-quote characters in your raw string delimiter to exceed any consecutive run of quote characters in the content (CS8998). The delimiter must contain more consecutive quotes than any sequence within the string content so the compiler can unambiguously distinguish between quote characters that are part of the content and the delimiter sequence that marks the end of the string.
  • For interpolated raw string literals, ensure the number of dollar signs ($) at the start matches the number of consecutive opening or closing braces you need as literal content (CS9005, CS9006, CS9007). The interpolated raw string syntax uses the dollar sign count to determine the brace escape sequence length, so $$""" requires {{ for interpolation holes and allows single { characters as content, while mismatched brace sequences indicate either incorrect interpolation syntax or content that needs a different dollar sign count.
  • Remove the @ prefix from your raw string literal and use only the quote character delimiter (CS9008, CS9009). Raw string literals are a distinct syntax introduced in C# 11 that doesn't use the @ verbatim string prefix, and the language specification doesn't allow combining the @ verbatim syntax with raw string delimiters because raw strings already support multi-line content and don't require escape sequences.

Note

CS9001 is no longer produced in current versions of C#. Multi-line raw string literals now support interpolation without requiring verbatim format.

For more information, see raw string literals.

UTF-8 string literals

  • CS9026 - The input string cannot be converted into the equivalent UTF-8 byte representation.
  • CS9047 - Operator cannot be applied to operands that are not UTF-8 byte representations.

To correct these errors, apply the following techniques:

  • Remove characters or escape sequences that can't be encoded in UTF-8 from your u8 string literal (CS9026). The UTF-8 encoding specification supports the full Unicode character set but requires valid Unicode scalar values, so surrogate code points (values in the range U+D800 through U+DFFF) can't appear directly in UTF-8 strings because they're reserved for UTF-16 surrogate pair encoding rather than representing standalone characters, and attempting to encode them as UTF-8 would produce an invalid byte sequence.
  • Ensure both operands of the addition operator are UTF-8 string literals (marked with the u8 suffix) when concatenating UTF-8 strings (CS9047). The compiler provides special support for concatenating UTF-8 string literals at compile time, which produces ReadOnlySpan<byte> values representing the concatenated UTF-8 byte sequences, but mixing UTF-8 strings with regular string values or other types isn't supported because the type system can't determine whether to produce a byte span or a text string, and the underlying representations (UTF-8 bytes versus UTF-16 characters) are fundamentally incompatible.

For more information, see UTF-8 string literals.

User string heap and data section limits

  • CS8103: Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals.
  • CS9274: Cannot emit this string literal into the data section because it has XXHash128 collision with another string literal.
  • CS9315: Combined length of user strings used by the program exceeds allowed limit. Adding a string literal requires restarting the application.

The common language infrastructure (CLI) metadata specification limits the size of the UserString heap (the metadata heap where string literals are stored in a compiled assembly) to 2^24 bytes (approximately 16.7 MB of UTF-16 characters). When the combined size of all string literals in a single project exceeds this threshold, the compiler generates CS8103 (or CS9315 during an Edit and Continue debugging session).

This error commonly occurs in projects with large generated source files (such as Entity Framework models or T4 templates), web projects with extensive Razor (.cshtml) views containing inline HTML and scripts, or applications that embed large JSON, SQL, or XML payloads directly as C# string literals.

To resolve these diagnostics, apply the following techniques:

  • Move large blocks of text (such as JSON schemas, HTML templates, or SQL scripts) into external files or embedded resources loaded at runtime, rather than defining them as inline string literals (CS8103).

  • Refactor large projects into multiple smaller class library projects (CS8103). The 16.7 MB UserString heap limit applies per compiled assembly (.dll or .exe), so distributing code across assemblies distributes the metadata string load.

  • Enable the experimental data section string literal feature in your project file (CS8103). This compiler option emits long string literals into a PE data section instead of the UserString heap:

    <PropertyGroup>
      <Features>$(Features);experimental-data-section-string-literals</Features>
    </PropertyGroup>
    
  • Disable the experimental data section string literals feature if your project encounters a hash collision (CS9274). This error indicates that two distinct string literals produced the same XXHash128 value, preventing the optimization from functioning.

  • Restart your application when you get CS9315 during an Edit and Continue (Hot Reload) session. Each applied update adds its string literals to the running module's metadata, so the strings accumulated across the session can exceed the UserString heap limit even if the project builds without error. When an update needs a new string literal and the compiler can't emit it into either the UserString heap or a data section, it reports CS9315. Restarting rebuilds the assembly and resets the accumulated metadata.