How to Resolve Error "CS0262: Partial declarations of type have conflicting accessibility modifiers" in C#
The Compiler Error CS0262 is a consistency error regarding Access Modifiers. The message reads: "Partial declarations of 'Type' have conflicting accessibility modifiers".
In C#, the partial keyword allows you to define a class, struct, or interface across multiple files. When the compiler merges these parts into a single type definition, the Access Level (Visibility) must be identical in every file. A class cannot be public in one file and internal in another; it must be consistently defined.
This guide explains how to align your modifiers to resolve this conflict.
Understanding Partial Consistency
When you split a class into File1.cs and File2.cs, they represent the same logical entity. Therefore, they must agree on their visibility to the outside world.
- Allowed: Both are
public. - Allowed: Both are
internal. - Allowed: One is
public, and the other omits the modifier (BUT only if the default matches the explicit one, which is rare for public types). - Error: One is
public, the other isinternal.
Default Visibility:
Top-level classes (not nested inside another class) are internal by default if no modifier is specified.
Nested classes are private by default.
Scenario 1: Explicit Modifier Conflict
This occurs when you explicitly type different keywords in different files.
Example of error
File 1 (User.cs):
// Explicitly Public
public partial class User
{
public string Name { get; set; }
}
File 2 (User.Methods.cs):
// ⛔️ Error CS0262: Partial declarations have conflicting modifiers.
// This part claims the class is Internal.
internal partial class User
{
public void Save() { }
}
Solution: Align the Modifiers
Decide on the intended visibility and apply it to all parts.
File 2 (Fixed):
// ✅ Correct: Matches File 1
public partial class User
{
public void Save() { }
}
Scenario 2: The Implicit Default Trap
This is the most common cause of CS0262. You define a class as public in one file, but in the second file (often a generated file or a quick extension), you forget to type public.
Because C# defaults top-level classes to internal, omitting the keyword creates a conflict between public (explicit) and internal (implicit).
Example of error
File 1 (Configuration.cs):
// Explicitly Public
public partial class Configuration
{
public string Path { get; set; }
}
File 2 (Configuration.Helpers.cs):
// ⛔️ Error CS0262: Missing modifier means 'internal'.
// 'public' vs 'internal' conflict.
partial class Configuration
{
public void Load() { }
}
Solution: Be Explicit Everywhere
Always explicitly state the access modifier on partial classes to avoid relying on defaults.
File 2 (Fixed):
// ✅ Correct: Added 'public' to match the other declaration
public partial class Configuration
{
public void Load() { }
}
Auto-Generated Code (WPF/WinForms):
If File 1 is auto-generated (like MainWindow.g.i.cs), check its definition. If the generator made it public, your code-behind (MainWindow.xaml.cs) must also be public.
Conclusion
CS0262 ensures your class definition is not schizophrenic.
- Check All Parts: Find every file defining the partial class (
Shift + F12or "Find All References"). - Check Keywords: Ensure every file starts with the exact same modifier string (e.g.,
public partial class). - Watch Defaults: Remember that omitting the keyword equals
internal(for top-level types), which will conflict if another part ispublic.