Skip to main content

How to Resolve Error "CS1038: #endregion directive expected" in C#

The Compiler Error CS1038 is a syntax error related to the C# Preprocessor. The message reads: "#endregion directive expected".

In C#, the #region and #endregion directives allow you to collapse and organize blocks of code in the IDE (like Visual Studio). These directives function as a pair of brackets. If the compiler encounters a #region directive but reaches the end of the file without finding a matching #endregion, it raises CS1038. This indicates that a region was opened but never closed.

This guide explains how to balance your region directives.

Understanding Region Blocks

Regions are purely for developer convenience (code folding) and do not affect the compiled binary. However, the preprocessor parser enforces strict balancing.

Valid Structure:

#region My Region Name
// Code...
#endregion

If you omit the closing tag, the preprocessor considers the rest of the file to be "inside" the region, until it runs out of lines and realizes the closure is missing.

Scenario 1: Missing the Closing Directive

This is the most common cause. You created a region to group some methods or properties, but forgot to type the closing directive, or accidentally deleted it while refactoring.

Example of error

public class User
{
public int Id { get; set; }

// ⛔️ Error CS1038: #endregion directive expected
// The region starts here, but the file ends before it closes.
#region User Methods

public void Save() { }
public void Delete() { }

} // End of Class

Solution: Add #endregion

Insert the closing directive where the logical block ends.

public class User
{
public int Id { get; set; }

// ✅ Correct: Region is opened...
#region User Methods

public void Save() { }
public void Delete() { }

// ✅ Correct: ...and closed.
#endregion

} // End of Class
tip

Debugging Tip: In Visual Studio, try to collapse your regions by clicking the - (minus) sign in the left margin. If a region collapses the entire remainder of the file (swallowing code below it that shouldn't be inside), that is likely the region missing its #endregion.

Scenario 2: Hiding Directives with #if

Preprocessor directives can interact with conditional compilation (#if). If you place an #endregion directive inside a conditional block (e.g., #if DEBUG) that evaluates to false, the preprocessor ignores that line entirely. As a result, the #region declared outside that block remains unclosed.

Example of error

Hiding the closing tag inside a disabled block.

#region Debugging Tools

#if DEBUG
public void Log() { Console.WriteLine("Log"); }

// ⛔️ Error CS1038: If we are in RELEASE mode, the #if DEBUG block is ignored.
// That means this #endregion is ALSO ignored.
// The outer #region therefore never closes.
#endregion
#endif

Solution: Balance Scopes

Ensure that if you open a region outside a conditional block, you close it outside the conditional block.

#region Debugging Tools

#if DEBUG
public void Log() { Console.WriteLine("Log"); }
#endif

// ✅ Correct: This is visible regardless of build configuration.
#endregion

Conclusion

CS1038 is a simple balancing error.

  1. Count Them: For every #region, there must be an #endregion.
  2. Check Visibility: Ensure your #endregion isn't hidden inside a disabled #if block.
  3. Check Footer: If the error occurs at the very last line of the file, search upwards for the last #region you added.