Skip to main content

How to Resolve Error "CS1027: #endif directive expected" in C#

The Compiler Error CS1027 is a preprocessor syntax error. The message reads: "#endif directive expected".

In C#, preprocessor directives (commands starting with #) allow you to conditionally compile specific blocks of code. The most common directive is #if, which checks a symbol (like DEBUG). A fundamental rule of these directives is that every #if must have a corresponding closing #endif.

This error occurs when the compiler reaches the end of the file (or the end of a preprocessor block) and finds an open #if that was never closed.

This guide explains how to balance your preprocessor directives to resolve this error.

Understanding Conditional Compilation

Conditional compilation looks like this:

#if SYMBOL
// Code A
#else
// Code B
#endif

If you omit the #endif, the compiler doesn't know where "Code A" (or B) stops. It will continue treating the rest of the file (and sometimes subsequent files in the compilation order) as part of that conditional block until it hits the end of the stream, at which point it panics with CS1027.

Scenario 1: Missing the Closing Directive

This is the most direct cause. You opened a conditional block to write some debug-only code but forgot to close it.

Example of error

public class Logger
{
public void Log(string message)
{
// We start a conditional block
#if DEBUG
System.Console.WriteLine($"[DEBUG]: {message}");

// ⛔️ Error CS1027: #endif directive expected
// The file ends here. The compiler is still waiting for the #endif.
}
}

Solution: Add #endif

Locate the end of the code block that should be conditional and insert the directive.

public class Logger
{
public void Log(string message)
{
#if DEBUG
System.Console.WriteLine($"[DEBUG]: {message}");
// ✅ Correct: The block is properly closed.
#endif
}
}

Scenario 2: The #else if Trap (vs #elif)

In languages like C++ or JavaScript (logic), "else if" is a standard phrase. In C# preprocessor directives, the keyword for "else if" is #elif.

If you mistakenly write #else followed by #if on a new line, you are actually creating a nested conditional block. This requires two #endif directives to close. If you only provide one, the outer block remains open, triggering CS1027.

Example of error

#define WINDOWS

public class Config
{
void Setup()
{
#if WINDOWS
// Windows setup
#else // 1. This starts the else block
#if LINUX // 2. This starts a NEW nested if block
// Linux setup
#endif // 3. This closes the inner LINUX block
// 4. The outer WINDOWS block is STILL OPEN!
}
}
// ⛔️ Error CS1027: #endif directive expected (for the first #if)

Solution: Use #elif

Use the combined keyword to keep the logic in a single chain.

#define WINDOWS

public class Config
{
void Setup()
{
#if WINDOWS
// Windows setup
// ✅ Correct: #elif continues the same chain.
#elif LINUX
// Linux setup
#endif // This one #endif closes the whole chain.
}
}

Conclusion

CS1027 is a simple balancing error.

  1. Count Them: For every #if, there must be exactly one #endif.
  2. Check Syntax: Ensure you are using #elif for "else if" logic, not #else followed by #if.
  3. Check Footer: Scroll to the bottom of your file. If the error points to the very last line, you have an unclosed block somewhere above it.