How to Resolve Error "CS0763: Both partial member declarations must be static or neither may be static" in C#
The Compiler Error CS0763 is a signature mismatch error regarding Partial Methods. The message reads: "Both partial member declarations must be static or neither may be static".
In C#, a partial method is defined in two parts (usually across two files): the Defining Declaration (the signature) and the Implementing Declaration (the body). The compiler merges these into a single method during compilation. For this merge to be valid, the method signature must be identical in both places. This includes the static modifier. If one declaration is marked static and the other is not, the compiler considers them incompatible.
This guide explains how to align your partial method declarations.
Understanding the Static Mismatch
When you declare a method, static fundamentally changes how it works:
- Static: Belongs to the type. Cannot access
this. Called viaClassName.Method(). - Instance (Non-Static): Belongs to an object. Can access
this. Called viaobjectInstance.Method().
You cannot have a method that is defined as "Static" in one file but implemented as "Instance" in another. The compiler cannot reconcile these two different behaviors into one method.
Scenario: Inconsistent static Modifier
This error commonly occurs when you define a hook in a static partial class (where all methods must be static) but forget to add the keyword in the implementation file, or vice versa.
Example of error
File 1 (Definition):
public partial class Logger
{
// Defined as a static method
static partial void LogInternal(string msg);
public static void Log(string msg)
{
LogInternal(msg);
}
}
File 2 (Implementation):
public partial class Logger
{
// ⛔️ Error CS0763: Both partial member declarations must be static or neither...
// This implementation is missing 'static', making it an Instance method.
partial void LogInternal(string msg)
{
System.Console.WriteLine(msg);
}
}
Solution 1: Add static to Both (Global Utility)
If the method is intended to be a utility function that does not need access to instance data (fields/properties), ensure static is present in both files.
File 2 (Fixed Implementation):
public partial class Logger
{
// ✅ Correct: Added 'static' to match File 1.
static partial void LogInternal(string msg)
{
System.Console.WriteLine(msg);
}
}
Solution 2: Remove static from Both (Instance Method)
If the method needs to access instance fields (using this), or if the containing class is not static, you should remove the static keyword from the definition in File 1.
File 1 (Fixed Definition):
public partial class Logger
{
// ✅ Correct: Removed 'static'. Now it is a standard instance method.
partial void LogInternal(string msg);
public void Log(string msg)
{
LogInternal(msg);
}
}
If the containing class is declared as static partial class, all members must be static. In that case, Solution 2 is not an option; you must use Solution 1.
Conclusion
CS0763 enforces consistency in method behavior.
- Check the Definition: Look at the file ending with
;. Is itstatic? - Check the Implementation: Look at the file ending with
{ ... }. Is itstatic? - Align Them: Make sure the
statickeyword appears in both locations or neither location.