Why does the C# compiler report CS0411 for `int id = Wire.Decode(field);` when `Decode<T>(string field)` returns `T`, and what does inference use instead?
answer
- arguments against parameter types
- assignment target is not an input
- where clauses are checked afterwards
- lambda return types do count
- all type arguments or none
basics
~20 sC# infers type arguments only from the arguments matched against parameter types, plus lambda return types and out variables. T appears only in Decode's return type, and neither the return type, the assignment target nor a constraint feeds inference, so it fails with CS0411.
solid answer
~40 sType-argument inference runs at compile time, before overload resolution, and pairs each argument's static type with its parameter's declared type. It also reads a lambda's return type when the parameter is a delegate such as `Func<T>`, and the declared type of an `out` variable. It never reads the method's return type or the assignment target, so `int id = Decode(field)` gives no bound for `T`; it never reads `where` clauses, so a `TItem` that appears only in a constraint stays unknown; and `null` carries no type. Conflicting bounds are resolved only if one converts to the others: `Pick(1, 2L)` gives `long`, `Pick(1, "one")` fails. The fix is explicit type arguments (all of them, or CS0305) or a signature where `T` appears in a parameter.
code
csharp · 13 linesstring field = "42";
int id = Wire.Decode(field); // not inferred from the return type
var first = Wire.FirstOf(new List<string> { "ping" }); // TItem appears only in a constraint
Wire.Send(null); // null carries no type
var mixed = Wire.Pick(1, "one"); // int and string: no single T fits
static class Wire
{
public static T Decode<T>(string field) => throw new NotImplementedException();
public static TItem FirstOf<TList, TItem>(TList list) where TList : IEnumerable<TItem> => list.First();
public static void Send<T>(T message) { }
public static T Pick<T>(T first, T second) => first;
}go deeper
Know that a generic call usually needs no angle brackets because the compiler reads the argument types, and that CS0411 means you must write them.
Explain exactly what feeds inference: argument types, lambda return types and out variables, never the return type, the target or constraints, and that explicit type arguments are all or nothing.
In code review, spot generic signatures whose type parameter appears only in the return type or a constraint and reshape them so callers get inference, or explicit arguments that document intent.
Treat inferability as part of a library's public API quality: signatures that infer cleanly read like non-generic calls, which is the argument the CA1000 analyzer rule makes.
## What type-argument inference is When you call a **generic method** without angle brackets, as in `Wire.Pick(1, 2L)`, the C# compiler runs **type-argument inference** to work out the missing type arguments. It happens entirely at compile time, once per call, and **before overload resolution**: the compiler tries inference on every generic method of that name, and only the candidates whose inference succeeded go on to compete as overloads. If inference fails for the method you meant, you get **CS0411**: "The type arguments for method '...' cannot be inferred from the usage. Try specifying the type arguments explicitly." ## What inference reads, and what it ignores Inference pairs **each argument** with **its parameter's declared type** and collects bounds for every type parameter that appears there. Concretely, the inputs are: - the **static types of the arguments** (`1` is `int`, `2L` is `long`, a `List<string>` variable is `List<string>`); - the **return type of a lambda or method group** passed where the parameter is a delegate such as `Func<T>`, once that delegate's parameter types are known; - the **declared type of an `out` variable**, so `TryDecode("7", out int seq)` infers `T` as `int`. Three things it never reads: 1. **The method's own return type or the assignment target.** `int id = Wire.Decode(field);` gives the compiler nothing: `T` appears only in the return type, and the `int` on the left is not an input to inference. The C# programming guide states it directly: the compiler "cannot infer the type parameters only from a constraint or return value", so a method with no parameter that mentions `T` can never be called without explicit type arguments. 2. **Constraints.** In `TItem FirstOf<TList, TItem>(TList list) where TList : IEnumerable<TItem>`, the argument fixes `TList` as `List<string>`, but `TItem` appears only in the `where` clause, which inference does not consult. The call fails with CS0411 even though a human can see `TItem` must be `string`. Constraints are checked **after** inference, against the types it produced. 3. **Arguments without a type.** The `null` literal and the `default` literal carry no type, so `Send(null)` gives no bound for `T` and fails. ## How competing bounds are resolved When several arguments bound the same `T`, the compiler picks a candidate that every other bound converts to: | Call | Bounds on `T` | Result | |---|---|---| | `Pick(1, 2)` | `int`, `int` | `T` = `int` | | `Pick(1, 2L)` | `int`, `long` | `T` = `long`, because `int` converts implicitly to `long` | | `Pick(1, "one")` | `int`, `string` | CS0411: neither converts to the other | | `Run(() => 42)` | lambda returns `int` | `T` = `int`, from the lambda's return type | | `Send(null)` | none | CS0411: `null` has no type | Note that `Pick(1, "one")` does **not** quietly fall back to `object`: inference only chooses among the types it actually saw. ## Fixing a CS0411 - **Supply the type arguments explicitly**: `Wire.Decode<int>(field)`. It is all or nothing: for a method with two type parameters, `FirstOf<List<string>>(names)` is rejected with **CS0305** ("Using the generic method ... requires 2 type arguments"). - **Give the type a parameter to flow through.** Rewrite `FirstOf<TList, TItem>` as `FirstOf<TItem>(IEnumerable<TItem> items)`; now `TItem` appears in a parameter type and is inferred from `List<string>` as `string`. - **Turn a return-only `T` into an `out` parameter** when the call site already names the type: `bool TryDecode<T>(string field, out T value)` lets callers write `TryDecode(field, out int id)`. - **Type the untyped argument**: `Send((string?)null)` or `Send<string?>(null)`. ## Why it matters in code review Inference failures are a design signal more than a nuisance. A public generic method whose type parameter appears only in its return type forces every caller to write the type argument. The code-analysis rule CA1000 ("Do not declare static members on generic types") makes the same argument: it asks you to avoid both a static member on a generic type and a generic method that does not support inference, so that calling a generic member reads like calling a non-generic one. Conversely, when inference picks a wider type than you intended, as `long` from `Pick(1, 2L)`, writing the type argument explicitly documents the choice. How the chosen candidate then wins or loses against other overloads is a matter for overload resolution, which runs on the inferred signature.
- Why does `Pick(1, "one")` fail instead of inferring `object`?Inference chooses only among the types it collected as bounds, here `int` and `string`, and picks one that every other bound converts to. Neither converts to the other, so there is no candidate and the call fails with CS0411. `object` was never a bound, so it is never considered; write `Pick<object>(1, "one")` if that is what you want.
- How can a `Decode`-style API avoid forcing callers to write the type argument?Make `T` appear in a parameter. A Try-pattern signature such as `bool TryDecode<T>(string field, out T value)` lets the caller write `TryDecode(field, out int id)`, and inference reads `int` from the out variable's declared type. Alternatively accept a typed argument, such as a default value of type `T`.
- When does type-argument inference run relative to overload resolution?First. The compiler runs inference for every generic method with the called name; methods whose inference fails are dropped, and the survivors, with their inferred signatures, compete in overload resolution alongside any non-generic candidates. That is why a CS0411 can disappear when another applicable overload exists.
- the compiler infers T from the variable you assign the result to
- a where constraint lets the compiler work out the type argument
- Pick(1, "one") infers object as the common type
- you can supply just the first type argument and infer the rest
- inference happens at run time from the actual argument values