How to Resolve Error "CS1025: Single-line comment or end-of-line expected" in C#
The Compiler Error CS1025 is a syntax error related to Preprocessor Directives. The message reads: "Single-line comment or end-of-line expected".
In C#, lines starting with a hash symbol (#) are processed by the preprocessor before the actual compilation. Unlike standard C# code, which ends with semicolons, preprocessor directives end at the new line. This error occurs when the compiler finds text, code, or invalid characters on the same line after a directive that doesn't accept arguments (like #else or #endif), or when using an invalid comment style.
This guide explains the strict formatting rules for preprocessor directives.
Understanding Preprocessor Line Rules
Directives like #region accept text arguments (the region name). However, directives like #endif, #else, and #endregion do not accept arguments.
The only thing allowed after a no-argument directive is a Single-Line Comment (//).
- Valid:
#endif // Debug mode - Invalid:
#endif Debug mode(The compiler thinks "Debug mode" is code, which isn't allowed here).
Scenario 1: Text After #endif or #else
This is the most common cause. Developers often label their closing directives to indicate what block is ending, similar to how one might comment a closing brace } // End if. However, if you forget the //, the compiler rejects the text.
Example of error
#if DEBUG
public void Test() { }
// ⛔️ Error CS1025: Single-line comment or end-of-line expected.
// The compiler does not know what 'End of Debug' means here.
#endif End of Debug
Solution: Use a Comment
Add double slashes // before the label.
#if DEBUG
public void Test() { }
// ✅ Correct: The text is now properly commented out.
#endif // End of Debug
Scenario 2: Using Multi-line Comments
While C# code supports block comments (/* ... */), preprocessor directives strictly require single-line comments (//) if you want to add text at the end of the line. Using block comment syntax on a directive line is invalid in many contexts or strictly interpreted as "unexpected text" by the preprocessor parser.
Example of error
// ⛔️ Error CS1025: Preprocessor directives generally do not support /* */ syntax
// on the same line in this manner.
#region Settings /* Configuration Area */
Solution: Use Single-line Comments
Switch to //.
// ✅ Correct
#region Settings // Configuration Area
Scenario 3: Code on the Same Line
Unlike standard statements, you cannot put code on the same line as a directive. A directive consumes the entire line.
Example of error
Trying to condense code to one line.
// ⛔️ Error CS1025: The code 'Console.WriteLine...' is treated as garbage text
// belonging to the #if directive.
#if DEBUG Console.WriteLine("Debug Mode");
#endif
Solution: Use New Lines
Directives must be on their own lines.
// ✅ Correct
#if DEBUG
Console.WriteLine("Debug Mode");
#endif
Semicolons: Even adding a semicolon immediately after a directive (e.g., #define MY_FLAG;) will trigger this error or CS1024, because the semicolon is treated as invalid text, not a terminator.
Conclusion
CS1025 is the preprocessor telling you: "I stopped reading the command, but there is still stuff on this line."
- Check
#endif/#else: These take NO arguments. If you have text after them, ensure it starts with//. - Check Comments: Always use
//for comments on preprocessor lines, never/* */. - Check Formatting: Never put C# code (like methods or variables) on the same line as a
#directive.