How to Resolve Error "CS0758: Both partial member declarations must use a params parameter or neither may use a params parameter" in C#
The Compiler Error CS0758 is a signature mismatch error regarding Partial Methods. The message reads: "Both partial member declarations must use a params parameter or neither may use a params parameter".
In C#, a partial method consists of a Definition (signature) and an Implementation (body), often split across different files. The compiler requires the signatures in both locations to match exactly. This includes parameter types, names, and modifiers. If one part uses the params keyword (allowing variable arguments) and the other part omits it, the compiler treats them as different methods and raises CS0758.
This guide explains how to align the signatures of your partial methods.
Understanding Signature Matching
When you define a partial method, the compiler merges the two declarations into one. For this merge to happen, the "contract" must be identical on both sides.
- Definition:
partial void Log(params string[] args); - Implementation:
partial void Log(string[] args) { ... }
To the compiler, params string[] and string[] describe different calling conventions. One allows Log("A", "B"), while the other strictly requires Log(new string[] { "A", "B" }). Since the behavior differs, the signatures are incompatible.
Scenario: Inconsistent params Usage
This error usually occurs when a developer defines a partial method hook (often in a generated file or base file) using params for convenience, but forgets to include the keyword when implementing the logic in their specific file.
Example of error
File 1 (Definition):
public partial class Logger
{
// Defined with 'params' to allow flexible calling
partial void OnLog(params string[] messages);
public void Trigger()
{
OnLog("Error 1", "Error 2");
}
}
File 2 (Implementation):
public partial class Logger
{
// ⛔️ Error CS0758: Both partial member declarations must use a params parameter...
// We forgot 'params' here. The compiler sees a standard array parameter.
partial void OnLog(string[] messages)
{
foreach (var msg in messages)
{
System.Console.WriteLine(msg);
}
}
}
Solution 1: Add params to Both (Enable Variable Args)
If you want the functionality of variable arguments (passing a comma-separated list), ensure the params keyword is present in the implementation file.
File 2 (Fixed Implementation):
public partial class Logger
{
// ✅ Correct: Added 'params' to match the definition in File 1.
partial void OnLog(params string[] messages)
{
foreach (var msg in messages)
{
System.Console.WriteLine(msg);
}
}
}
Solution 2: Remove params from Both (Standard Array)
If you prefer to be explicit and always pass an array object, remove the params keyword from the definition in File 1.
File 1 (Fixed Definition):
public partial class Logger
{
// ✅ Correct: Removed 'params'. Now it matches the original implementation.
// Note: You must now update call sites to pass explicit arrays:
// OnLog(new string[] { "A", "B" });
partial void OnLog(string[] messages);
}
If File 1 is auto-generated (e.g., by a source generator or designer tool), you usually cannot edit it. In that case, you must use Solution 1 and adapt your implementation to match the generated code.
Conclusion
CS0758 enforces strict consistency in your code structure.
- Check the Definition: Look at the file where the method ends with a semicolon
;. Does it haveparams? - Check the Implementation: Look at the file where the method has a body
{ ... }. Does it haveparams? - Align Them: Make sure both sides either include the keyword or exclude it.