Skip to main content

How to Resolve Error "CS0267: The 'partial' modifier can only appear immediately before 'class', 'record', 'struct', 'interface', 'event', an instance constructor name, or a method or property return type." in C#

The Compiler Error CS0267 is a syntax and semantic error. The message reads: "The 'partial' modifier can only appear immediately before 'class', 'record', 'struct', 'interface', 'event', an instance constructor name, or a method or property return type."

In C#, the partial keyword is used to split the definition of a type or a method across multiple files. However, it is not a universal modifier. You cannot split a Field (variable), an Enum, or a Delegate across multiple files. Furthermore, because it modifies the specific type definition, the keyword partial must be placed in a specific position in the declaration syntaxβ€”usually right before the keyword class, struct, or the return type of a method.

This guide explains where partial is allowed and how to fix misplaced modifiers.

Understanding Valid Partial Targets​

The partial keyword allows you to extend definitions. However, C# limits what can be extended:

  • Allowed: class, struct, interface, record.
  • Allowed: void MethodName() (Partial Methods).
  • Allowed (C# 13+): Properties (Partial Properties).
  • Not Allowed: enum, delegate.
  • Not Allowed: int field, string variable (Fields/Data members).

If you try to apply partial to an unsupported item, or put the keyword in the wrong spot, CS0267 is triggered.

Scenario 1: Using partial on Fields (Variables)​

This is a common mistake when developers want to "declare" a variable in one file and "initialize" it in another file as part of the definition. C# does not support partial fields. A variable must be fully defined in one location.

Example of error​

public partial class User
{
// ⛔️ Error CS0267: The 'partial' modifier can only appear immediately before class...
// You cannot split a field definition.
public partial int UserCount;
}

Solution: Standard Field​

Define the field normally. Since the class itself is partial, other files can access this field freely.

public partial class User
{
// βœ… Correct: Just a standard field.
public int UserCount;
}

// In another file (User.Methods.cs):
public partial class User
{
public void Reset()
{
// We can access 'UserCount' here because the CLASS is partial.
UserCount = 0;
}
}
note

Partial Properties: In recent versions of C# (13+ / .NET 9), you can use partial properties (e.g., public partial string Name { get; set; }). If you are on an older version, this will raise CS0267.

Scenario 2: Using partial on Enums​

Developers often want to extend an enum (e.g., adding error codes from different files). C# does not support partial enums. An enum must be fully defined in a single file.

Example of error​

// File 1
public partial enum HttpStatus
{
OK = 200
}

// File 2
// ⛔️ Error CS0267: Enums cannot be partial.
public partial enum HttpStatus
{
NotFound = 404
}

Solution: Constant Classes​

If you need an extensible list of values, use a static class with const fields instead of an enum.

public static partial class HttpStatus
{
public const int OK = 200;
}

// In another file
public static partial class HttpStatus
{
public const int NotFound = 404;
}

Scenario 3: Syntax and Ordering​

The error specifically states that partial must appear immediately before specific keywords. While modern C# is lenient about the order of public, static, etc., partial usually anchors the type definition.

Example of error (Wrong position)​

Trying to place partial before the access modifier (in some contexts) or separating it from the method signature incorrectly.

public class Example
{
// ⛔️ Error CS0267: Syntax error.
// 'partial' typically modifies the return type 'void' or comes after access modifiers.
partial public void DoWork();
}

Solution: Correct Order​

Place partial immediately before the type (class) or the return type (void).

public class Example
{
// βœ… Correct: Access modifier -> partial -> return type
public partial void DoWork();
}

public partial class MyClass { } // βœ… Correct

Conclusion​

CS0267 enforces the structure of split definitions.

  1. Fields: You cannot make variables partial. Define them once.
  2. Enums: You cannot make enums partial. Use static classes with constants if you need to split files.
  3. Position: Ensure partial is placed just before class, struct, or void (for methods).