Skip to content

How to Resolve Error "CS1022: Type or namespace definition, or end-of-file expected" in C#

Fix CS1022 in C# by identifying mismatched braces, stray methods, or incorrect file structure that leave code outside valid namespace or type definitions.

The Compiler Error CS1022 is a structural syntax error. The message reads: "Type or namespace definition, or end-of-file expected".

This error occurs when the C# compiler is analyzing the "root" level of your source file (outside of any braces, or after a namespace has closed) and encounters code that doesn't belong there. In the global scope, C# expects only three things:

  1. A Type definition (class, struct, interface, enum, delegate).
  2. A Namespace definition (namespace MySpace { ... }).
  3. The end of the file.

If it finds a method, a variable assignment, or a stray character that isn't one of these, it raises CS1022. This is almost always caused by mismatched curly braces { }.

Understanding File Structure

A standard C# file looks like this:

using System;

// Global Scope: Can contain namespaces or types
namespace MyApp 
{
    // Namespace Scope: Can contain types
    class Program 
    {
        // Class Scope: Can contain members (Methods, Fields)
    }
}
// End of File (Expected here)

If you accidentally close the class or namespace too early, any remaining code falls into the "Global Scope". If that remaining code is a method or a variable, the compiler rejects it because methods cannot exist globally (except for C# 9 Top-Level Statements, which have specific rules).

Scenario 1: Extra Closing Braces (Premature Closure)

This is the most frequent cause. You have an extra } somewhere in your code. This closes the class or namespace earlier than you intended, leaving the rest of the code "stranded" outside.

Example of error

namespace MyApp
{
    public class User
    {
        public string Name { get; set; }
    } // Closes Class User

    } // <--- ⛔️ Error: Accidental Extra Brace!
      // This closes the namespace 'MyApp'.

    // The compiler thinks we are now at the end of the file/global scope.
    
    // ⛔️ Error CS1022: Type or namespace definition... expected.
    // The compiler sees 'public class Admin' and accepts it (classes can be global).
    // BUT, if we had a method here, or if the structure implies nested logic...
    
    // Let's look at a clearer case of stranded code:
    public void HelperMethod() 
    {
        // ...
    }
} // Final brace

In the example above, HelperMethod is now outside the namespace. While methods inside a class are fine, a method floating in global space triggers CS1022 (or CS0116). The extra brace caused the structure to desynchronize.

Solution: Match Your Braces

Format your document (in Visual Studio: Ctrl + K, Ctrl + D) to see indentation. If the indentation looks wrong (code shifts to the left unexpectedly), you have an extra brace.

namespace MyApp
{
    public class User
    {
        public string Name { get; set; }
    } // Closes Class

    // ✅ Correct: Removed the extra brace. 
    // Now 'HelperMethod' must be inside a class to be valid.
    public class Utilities
    {
        public void HelperMethod() { }
    }

} // Closes Namespace

Scenario 2: Methods Defined Outside a Class

Beginners sometimes try to write functions like in C++ or JavaScript, placing them outside of any class block.

Example of error

using System;

namespace CalculatorApp
{
    class Program
    {
        static void Main() { }
    }
}

// ⛔️ Error CS1022: Type or namespace definition expected
// You cannot write a function here. It must be inside a class.
void MyFunction()
{
    Console.WriteLine("Hello");
}

Solution: Move Code Inside a Class

Cut the method and paste it inside the class definition (before the closing brace of the class).

namespace CalculatorApp
{
    class Program
    {
        static void Main() { }

        // ✅ Correct: Method is now a member of 'Program'
        static void MyFunction()
        {
            Console.WriteLine("Hello");
        }
    }
}

Scenario 3: Syntax Errors in Top-Level Statements

C# 9.0 introduced Top-Level Statements, allowing you to write code without a Main method or class Program wrapper. However, this only applies to the entry point logic. You cannot mix Top-Level statements with standard namespace definitions in a way that violates structure.

Example of error

Placing code after a namespace block in a file that doesn't strictly follow Top-Level rules.

using System;

namespace MyLibrary
{
    public class Worker { }
}

// ⛔️ Error CS1022: Type or namespace definition expected
// Once you define a namespace block in a file, you typically cannot 
// define loose statements afterwards.
Console.WriteLine("This code is stranded.");

Solution: Top-Level Precedence

Top-level statements must appear before any namespace or type declarations.

using System;

// ✅ Correct: Top-level statements first.
Console.WriteLine("This code runs at startup.");

namespace MyLibrary
{
    public class Worker { }
}

Conclusion

CS1022 is the compiler telling you: "I finished reading your types and namespaces, but there is still code left over."

  1. Check Braces: Use Ctrl + ] to jump between matching braces. Ensure you haven't closed a class or namespace too early.
  2. Check Indentation: If your method starts at the far left column (no indentation), it is likely outside the class.
  3. Check Structure: Ensure all methods and fields are wrapped inside class or struct blocks.
Navigation