How to Resolve Error "CS1039: Unterminated string literal" in C#
The Compiler Error CS1039 is a syntax parsing error. The message reads: "Unterminated string literal".
This error occurs when the C# compiler finds the beginning of a string (indicated by a double quote "), but reaches the end of the file or the data stream without finding the corresponding closing quote. While similar to CS1010 ("Newline in constant"), CS1039 is often associated with Verbatim Strings (@"...") or Raw String Literals ("""...""") that span multiple lines and are never closed before the file ends.
This guide explains how to locate and close these runaway strings.
Understanding String Termination
Every string literal in C# must have a start and an end.
- Standard:
"Text" - Verbatim:
@"Text" - Raw:
"""Text"""
If the compiler hits the absolute end of your source code file while it thinks it is still "reading a string," it throws CS1039. This usually breaks the entire parsing of the file, leading to many subsequent errors (like missing braces or semicolons) because the compiler treated your code as part of the text string.
Scenario 1: Missing Closing Quote on Standard Strings
This is the simplest case. You typed the opening quote but forgot the closing one, usually at the very end of a file or statement.
Example of error
public class Program
{
static void Main()
{
// ⛔️ Error CS1039: Unterminated string literal
// The compiler sees the start ", but reaches the semicolon without a close ".
string message = "System Failure;
}
}
Solution: Close the Quote
Add the missing double quote.
public class Program
{
static void Main()
{
// ✅ Correct: The string is properly enclosed.
string message = "System Failure";
}
}
Scenario 2: Unclosed Verbatim Strings (The "EOF" Trap)
Verbatim strings (starting with @) are allowed to span multiple lines. If you open a verbatim string and forget to close it, the compiler swallows the rest of your file (classes, methods, namespaces) treating it all as string content. When it hits the physical end of the file (EOF), it raises CS1039.
This is tricky to debug because the error might point to the start of the string, while the missing quote is hundreds of lines down.
Example of error
public class DataRepository
{
public string GetQuery()
{
// We start a SQL query with @"
return @"SELECT *
FROM Users
WHERE IsActive = 1
// ⛔️ Error CS1039: We forgot the closing quote here!
// The compiler consumes the brackets below as part of the string.
}
} // End of file found while parsing string.
Solution: Locate the Start
- Look at the line number reported by the error.
- Go to that line (the start of the string).
- Scroll down to where the text logically ends and add
";.
public class DataRepository
{
public string GetQuery()
{
// ✅ Correct: The string is closed properly.
return @"SELECT *
FROM Users
WHERE IsActive = 1";
}
}
Scenario 3: Mismatched Raw String Literals
In C# 11+, Raw String Literals use three or more quotes ("""). A common mistake is opening a string with three quotes but trying to close it with two, or forgetting that the closing quotes must be on their own line for multi-line blocks.
Example of error
public class HtmlGen
{
public string GetHtml()
{
// Opened with 3 quotes
var html = """
<html>
<body></body>
</html>
""; // ⛔️ Error CS1039: Closed with only 2 quotes.
// The compiler is still waiting for the 3rd quote.
}
}
Solution: Match the Count
If you start with three quotes, you must end with three quotes.
public class HtmlGen
{
public string GetHtml()
{
// ✅ Correct: Opening and Closing counts match (3).
var html = """
<html>
<body></body>
</html>
""";
}
}
Conclusion
CS1039 means the compiler read until the end of the file looking for a closing quote that never came.
- Check Syntax Highlighting: In Visual Studio, strings are usually red or brown. If half your code file has suddenly turned that color, you have an unclosed string at the point where the color change starts.
- Check Verbatim Strings: Look for
@"patterns. These are the most likely culprits for swallowing large chunks of code. - Check Raw Strings: Ensure
"""is balanced at both ends.