How to Resolve Error "CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit" in C#
The Compiler Error CS0748 is a syntax consistency error regarding Lambda Expressions. The message reads: "Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit".
In C#, when defining a lambda expression (e.g., (x, y) => x + y), you have two choices regarding parameter types:
- Implicitly Typed: Let the compiler infer the types based on the delegate definition (
(x, y)). - Explicitly Typed: Manually declare the type for every parameter (
(int x, int y)).
You cannot mix these styles. You cannot declare the type for the first parameter but leave the second one to be inferred. It must be "all or nothing."
This guide explains the syntax rules for lambda parameters and how to unify your coding style.
Understanding Lambda Parameter Typing
The C# compiler is smart enough to infer types in most lambda scenarios.
- Implicit:
(a, b) => a + b- The compiler looks at the delegate (e.g.,
Func<int, int, int>) and knowsaandbareint.
- The compiler looks at the delegate (e.g.,
- Explicit:
(int a, int b) => a + b- You explicitly state the types.
CS0748 occurs if you write: (int a, b) => .... The compiler does not support partial inference in a parameter list.
Scenario: Mixing Explicit and Implicit Types
This error often happens when a developer tries to be helpful by typing the first parameter to clarify what the lambda does, but assumes the compiler will figure out the rest. It is also common in Event Handlers.
Example of error
Attempting to define a Func that adds two numbers, but only typing the first argument.
using System;
public class Program
{
static void Main()
{
// Defined delegate expects two integers
Func<int, int, int> add;
// ⛔️ Error CS0748: Inconsistent lambda parameter usage.
// Parameter 'a' is Explicit (int).
// Parameter 'b' is Implicit (missing type).
add = (int a, b) => a + b;
}
}
Example of error (Event Handlers)
// ⛔️ Error CS0748: Common mistake in UI programming
// You typed 'object sender', but forgot 'EventArgs e'.
button.Click += (object sender, e) => Console.WriteLine("Clicked");
Solution 1: Use Implicit Typing (Recommended)
In 99% of C# code, implicit typing is preferred. It is cleaner, less verbose, and easier to refactor if types change later. The compiler already knows the types from the variable declaration or method signature you are assigning to.
Solution
Remove the type name (int) entirely.
using System;
public class Program
{
static void Main()
{
Func<int, int, int> add;
// ✅ Correct: Both 'a' and 'b' are implicitly typed as 'int'.
add = (a, b) => a + b;
}
}
Solution (Event Handlers)
// ✅ Correct: Inferred as (object sender, EventArgs e)
button.Click += (sender, e) => Console.WriteLine("Clicked");
Solution 2: Use Explicit Typing (For Clarity)
Sometimes you want to be explicit. This is useful if:
- The code is complex and you want to document what the variables are.
- You are calling an overloaded method and the compiler cannot guess which overload to use without explicit types.
- You are using the modern C# feature allowing attributes on lambda parameters.
Solution: add the missing type to the second parameter so all parameters are typed.
using System;
public class Program
{
static void Main()
{
Func<int, int, int> add;
// ✅ Correct: Both parameters are explicitly typed.
add = (int a, int b) => a + b;
}
}
C# 10 Feature:
In modern C#, you can also use var explicitly in lambdas: (var a, var b) => a + b. However, you still cannot mix (int a, var b). You must be consistent.
Conclusion
CS0748 is a rule about consistency.
- Check the Parentheses: Look at the left side of the
=>arrow. - Choose a Style:
- No Types:
(x, y)(Best for brevity). - All Types:
(int x, int y)(Best for disambiguation).
- No Types:
- Don't Mix: Never write
(int x, y).