How to Resolve Error "CS0261: Partial declarations of type must be all classes, all structs, or all interfaces" in C#
The Compiler Error CS0261 is a type consistency error. The message reads: "Partial declarations of 'Type' must be all classes, all structs, or all interfaces".
In C#, the partial keyword allows you to split the definition of a type across multiple files. However, the kind of type definition must be identical in every file. You cannot define the first part of a type as a class and the second part as a struct or interface. The compiler needs to know exactly what kind of data structure it is building (Reference Type vs. Value Type) before it can merge the pieces.
This guide explains how to ensure consistency across partial definitions.
Understanding Type Consistency
When the compiler merges partial files, it checks the keywords used to declare the type. They must match exactly.
- File A:
public partial class Data - File B:
public partial struct Data - Result: CS0261. A
classis allocated on the Heap. Astructis allocated on the Stack. The compiler cannot merge these two fundamentally different concepts into one object.
Scenario 1: Mixing Classes and Structs
This error most commonly occurs during refactoring. You might decide to change a lightweight struct into a full class (or vice versa) but forget to update one of the files, especially if one file is auto-generated or hidden in a different folder.
Example of error
File 1 (User.cs):
namespace MyApp
{
// Defined as a CLASS
public partial class User
{
public string Name { get; set; }
}
}
File 2 (User.Methods.cs):
namespace MyApp
{
// ⛔️ Error CS0261: Partial declarations of 'User' must be all classes,
// all structs, or all interfaces.
// Defined here as a STRUCT.
public partial struct User
{
public void Save() { }
}
}
Solution: Align the Keywords
Decide whether the type should be a Reference Type (class) or Value Type (struct) and update all files to match.
File 2 (Fixed):
namespace MyApp
{
// ✅ Correct: Changed 'struct' to 'class' to match File 1.
public partial class User
{
public void Save() { }
}
}
Scenario 2: Records vs. Classes (C# 9+)
With the introduction of record types in C# 9, this error can also occur if you mix record syntax with standard class syntax. Even though a standard record is technically a reference type (like a class), the keywords must still align.
Example of error
File 1:
public partial record Person(string FirstName, string LastName);
File 2:
// ⛔️ Error CS0261: You cannot mix 'class' and 'record'.
public partial class Person
{
public int Age { get; set; }
}
Solution: Use 'record' consistently
Update all partial declarations to use the record keyword.
File 2 (Fixed):
// ✅ Correct: Keywords match.
public partial record Person
{
public int Age { get; set; }
}
Record Structs (C# 10+):
Similarly, you cannot mix partial record class with partial record struct. They must be identical.
Conclusion
CS0261 ensures that the fundamental architecture of your type is consistent.
- Check All Files: Locate every file where the type is defined (use "Find All References" or
Shift+F12on the class name). - Verify Keywords: Ensure that if one file says
class, they all sayclass. If one saysstruct, they all saystruct. - Check Generated Code: If you are modifying a type that has a
.Designer.csfile, ensure your manual changes match the type defined by the generator.