How to Resolve Error "CS0753: Only methods, classes, structs, or interfaces may be partial" in C#
The Compiler Error CS0753 is a syntax restriction error. The message reads: "Only methods, classes, structs, or interfaces may be partial."
In C#, the partial keyword allows you to split a definition across multiple source files. However, this feature is restricted to specific constructs. You can split Types (classes, structs, interfaces, records) and Methods. You cannot split data members (like Fields or Properties), Constructors, Enums, or Delegates.
This guide explains the limitations of the partial keyword and how to achieve similar results using alternative patterns.
Understanding Partial Support
The partial modifier indicates that other parts of the definition may exist elsewhere.
- Supported:
partial class,partial struct,partial interface,partial record.partial void Method()(or typed returns in C# 9+).
- Not Supported:
partial int MyField;(Data must be defined in one place).partial int MyProperty { get; set; }(Properties are treated as single units).partial enum MyEnum(Enums are simple integer mappings and cannot be split).partial MyConstructor()(Constructors initialize state; splitting them causes ordering ambiguity).
Scenario 1: Partial Properties and Fields
A common desire when using source generators or separating logic is to declare a property in one file and implement its getter/setter logic in another. C# does not support this directly.
Example of error
public partial class User
{
// ⛔️ Error CS0753: Only methods, classes... may be partial.
// You cannot split a property definition.
public partial string Name { get; set; }
// ⛔️ Error CS0753: Fields cannot be partial.
public partial int _id;
}
Solution: Use Partial Methods in Accessors
If you need to inject logic into a property from another file (e.g., "OnChanged" notification), define a standard property that calls a partial method.
File 1 (Auto-Generated or Base):
public partial class User
{
private string _name;
// Define hooks
partial void OnNameChanging(string newValue);
partial void OnNameChanged();
public string Name
{
get => _name;
set
{
OnNameChanging(value);
_name = value;
OnNameChanged();
}
}
}
File 2 (Your Logic):
public partial class User
{
// Implement hooks
partial void OnNameChanged()
{
System.Console.WriteLine("Name was updated!");
}
}
Scenario 2: Partial Constructors
You might want to add initialization logic to a class whose constructor is auto-generated (e.g., by a designer tool). Adding partial to a constructor is invalid.
Example of error
public partial class Window
{
// ⛔️ Error CS0753: Constructors cannot be partial.
public partial Window()
{
// ...
}
}
Solution: Use an Initialization Hook
Standard .NET patterns (like in Windows Forms or WPF) use a partial method often named InitializeComponent or OnCreated.
// File 1
public partial class Window
{
public Window()
{
// Perform standard init
InitializeInternal();
// Call the partial hook
OnCreated();
}
partial void OnCreated();
}
// File 2
public partial class Window
{
// ✅ Correct: Implementing the partial method, not the constructor.
partial void OnCreated()
{
System.Console.WriteLine("Window is ready.");
}
}
Scenario 3: Partial Enums
Developers often ask for partial enum to define enum values across multiple files (e.g., grouping error codes). C# does not support this because Enums are compiled into a single contiguous block of literal values.
Example of error
// ⛔️ Error CS0753: Enums cannot be partial.
public partial enum ErrorCodes
{
None = 0
}
Solution: Use Constants Class
If you need an extensible list of values, use a static class with const fields instead of an enum.
public static partial class ErrorCodes
{
public const int None = 0;
}
// In another file...
public static partial class ErrorCodes
{
public const int NotFound = 404;
public const int ServerError = 500;
}
Conclusion
CS0753 enforces the boundaries of code splitting.
- Check the Type: Are you trying to split a Property, Field, Enum, or Constructor?
- Use Partial Methods: For logic hooks (Properties/Constructors), delegate the work to a
partial void Method(). - Use Partial Classes: For data organization (Constants), use a
static partial classinstead of an enum.