How to Resolve Error "CS0755: Both partial method declarations must be extension methods or neither may be an extension method" in C#
The Compiler Error CS0755 is a signature mismatch error regarding Partial Methods. The message reads: "Both partial method declarations must be extension methods or neither may be an extension method".
In C#, a partial method consists of two parts: the Definition (signature) and the Implementation (body). The compiler requires these two parts to match exactly. This includes the parameter modifiers. If you define one part as an Extension Method (using the this keyword on the first parameter) but forget to include this in the other part, the compiler considers them fundamentally different methods and raises CS0755.
This guide explains how to align your partial extension method signatures.
Understanding Partial Extension Methods
An extension method allows you to "add" methods to existing types. They are defined by adding the this keyword to the first parameter of a static method.
For a partial method to also be an extension method, it must adhere to specific rules:
- The containing class must be
staticandpartial. - The method must be
static. - Both the definition (in File A) and the implementation (in File B) must include the
thiskeyword.
Scenario: Mismatching the this Modifier
This error occurs when you define a partial method intended to be an extension method, but you inconsistently apply the this modifier across the two files.
Example of error
File 1 (Definition): Here, the developer defines it as an extension method.
public static partial class StringExtensions
{
// Defined as an extension method for 'string'
public static partial void Log(this string message);
}
File 2 (Implementation):
Here, the developer implements it as a standard static method, forgetting the this keyword.
public static partial class StringExtensions
{
// ⛔️ Error CS0755: Both partial method declarations must be extension methods...
// Missing 'this' before 'string message'.
public static partial void Log(string message)
{
System.Console.WriteLine(message);
}
}
Solution 1: Add this to Both Parts
If you intend for the method to be an extension method (callable like "Hello".Log()), ensure the implementation signature matches the definition exactly.
// File 2 (Fixed Implementation)
public static partial class StringExtensions
{
// ✅ Correct: Added 'this' to match File 1.
public static partial void Log(this string message)
{
System.Console.WriteLine(message);
}
}
Solution 2: Remove this (Standard Static Method)
If you strictly want a static utility method (callable like StringExtensions.Log("Hello")) and do not need the extension syntax, remove this from the definition in File 1.
// File 1 (Fixed Definition)
public static partial class StringExtensions
{
// ✅ Correct: Removed 'this'. It is now a normal static method.
public static partial void Log(string message);
}
Requirement: Remember that extension methods (and therefore partial extension methods) must essentially be declared in a static class. If your class is not static, you cannot use this on parameters at all.
Conclusion
CS0755 enforces consistency in method signatures.
- Check the Definition: Does the signature in the generated/base file use
this Type name? - Check the Implementation: Does your manually written code also use
this Type name? - Align Them: Either add
thisto both to enable extension syntax, or remove it from both to use standard static method syntax.