Skip to main content

How to Resolve Error "CS0263: Partial declarations of type must not specify different base classes" in C#

The Compiler Error CS0263 is an inheritance conflict error involving Partial Classes. The message reads: "Partial declarations of 'MyClass' must not specify different base classes".

In C#, the partial keyword allows you to split the definition of a single class across multiple files (commonly used in UI frameworks like WPF, WinForms, or ASP.NET). When these parts are merged during compilation, the class must act as a single, coherent unit. One fundamental rule of C# is that a class can inherit from only one base class. If one part of your partial class claims to inherit from ClassA, and another part claims to inherit from ClassB, the compiler faces a contradiction and raises CS0263.

This guide explains the rules of inheritance with partial types and how to resolve these conflicts.

Understanding Partial Class Inheritance

When you define a class in pieces:

  • Merge: All parts (User.cs, User.Designer.cs) are combined into one class.
  • Base Class: The combined class can have exactly one parent (Base Class).
  • Syntax: You can specify the base class on any of the partial declarations, or all of them, but if you specify it multiple times, it must be the same class.

Valid:

  • Part A: partial class User : BaseUser | Part B: partial class User (Inherits BaseUser)
  • Part A: partial class User : BaseUser | Part B: partial class User : BaseUser (Redundant but valid)

Invalid (CS0263):

  • Part A: partial class User : BaseUser | Part B: partial class User : OtherBase

Scenario 1: Conflicting Base Classes

This is the direct cause of the error. Two developers (or a developer and a code generator) disagree on what the parent class should be.

Example of error

File 1: User.cs

// Developer defines this part
public partial class User : Person
{
public string Name { get; set; }
}

File 2: User.Extra.cs

// Developer defines this part elsewhere
// ⛔️ Error CS0263: Partial declarations of 'User' must not specify different base classes.
// 'Employee' is different from 'Person'.
public partial class User : Employee
{
public int Salary { get; set; }
}

Solution: Consolidate the Base Class

Ensure only one base class is used. If Employee inherits from Person, you should inherit from the most specific one (Employee), or reorganize your hierarchy.

Corrected File 2:

// ✅ Correct: Both parts agree on 'Employee'.
// (Assuming Employee is the intended parent)
public partial class User : Employee
{
public int Salary { get; set; }
}

Corrected File 1:

// ✅ Correct: It is cleaner to omit the base class here if it is declared in File 2.
public partial class User // : Employee (implied by merger)
{
public string Name { get; set; }
}

Scenario 2: Redundant but Consistent Base Classes

Listing the same base class in multiple files is valid C#, though often redundant.

// File 1
public partial class Manager : Employee { }

// File 2
public partial class Manager : Employee { } // ✅ Valid

However, listing the base class in one file and an interface in another is also valid, provided the base class remains consistent (or omitted).

// File 1
public partial class Manager : Employee { }

// File 2
public partial class Manager : IDisposable { } // ✅ Valid (Base + Interface)

Scenario 3: Auto-Generated Code Conflicts

This error frequently occurs in ASP.NET, WPF, or WinForms projects.

  1. Generated Side: The framework generates a partial class that inherits from a specific system type (e.g., System.Web.UI.Page or System.Windows.Window).
  2. User Side: You create the code-behind file and manually try to change the inheritance to a custom base class.

Example of error (ASP.NET Example)

User.aspx.cs (Your Code):

// You want all pages to inherit from your custom 'MyBasePage'
public partial class UserProfile : MyBasePage
{
}

User.aspx.designer.cs (Generated Code):

// The tool generated this based on the markup
public partial class UserProfile : System.Web.UI.Page
{
}

Result: MyBasePage vs System.Web.UI.Page -> CS0263.

Solution: Update the Source of Truth

You cannot fix this just by changing the C# file, because the generator will overwrite its side or conflict with it. You must update the definition that drives the generator.

  • ASP.NET WebForms: Update the Inherits attribute in the .aspx file. <%@ Page Inherits="MyProject.MyBasePage" ... %>
  • WPF/XAML: Change the root tag in the .xaml file. <local:MyBaseWindow x:Class="MyProject.MainWindow" ... > instead of <Window ...>

Once you update the markup, the tool re-generates the designer file with the correct base class, resolving the conflict.

Conclusion

CS0263 ensures your class hierarchy is consistent.

  1. Check all Files: Look for every file defining partial class YourName.
  2. Verify Base Classes: Ensure they either specify the same base class or omit the base class (letting one file define it).
  3. Check Generators: If using a framework (XAML, ASPX), update the markup file, not just the C# code.