Skip to main content

Fixed: CS0411 The type arguments for method 'CollectionExtensions.GetValueOrDefault<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey)' cannot be inferred from the usage

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

My C# build stopped with error CS0411: The type arguments for method 'CollectionExtensions.GetValueOrDefault' cannot be inferred from the usage. I had called GetValueOrDefault on a variable declared as IDictionary<string, string>. That GetValueOrDefault is an extension method for IReadOnlyDictionary<TKey, TValue>, and IDictionary<TKey, TValue> isn't one. Declaring the variable with the read-only interface fixes it.

What I Was Doing​

I was writing a small Microsoft 365 Agents SDK agent in C# on .NET 10. Another program calls the agent over HTTP with values in the address, such as ?number=42&status=passed, and the SDK hands those values to my handler in turnContext.Activity.Value, typed as object. I cast it to a dictionary and read the two values:

BuildNotifierAgent.cs (BuildFinishedAsync, first version)
var values = turnContext.Activity.Value as IDictionary<string, string> ?? new Dictionary<string, string>();

if (!values.TryGetValue("number", out string? number) || !int.TryParse(number, out _))
{
throw new ArgumentException("Add the build number to the address, as number=...");
}

string message = values.GetValueOrDefault("status") switch
{
"passed" => $"✅ Build {number} passed.",
"failed" => $"❌ Build {number} failed.",
_ => throw new ArgumentException("Add the build's status to the address, as status=passed or status=failed")
};

GetValueOrDefault returns the value for a key, or null when the key isn't there, which suited the switch.

What I Expected​

A clean dotnet build.

What I Got​

Output of dotnet build
Restore complete (0.3s)
BuildNotifier net10.0 failed with 1 error(s) (0.2s)
/Users/you/source/BuildNotifier/BuildNotifierAgent.cs(39,33): error CS0411: The type arguments for method 'CollectionExtensions.GetValueOrDefault<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Build failed with 1 error(s) in 0.9s

A macOS-style terminal window: dotnet build of BuildNotifier fails with 1 error, error CS0411 at BuildNotifierAgent.cs line 39, The type arguments for method CollectionExtensions.GetValueOrDefault cannot be inferred from the usage, Try specifying the type arguments explicitly

Line 39 is the GetValueOrDefault call. The TryGetValue call on the same variable, a few lines up, compiled without complaint.

What I Checked​

What CS0411 means. Microsoft's C# reference: This error occurs when you call a generic method without explicitly providing the type arguments and the compiler can't infer which type arguments you intend. The compiler infers type arguments from the types of the method arguments you pass at the call site.

Which GetValueOrDefault it was. The message names it: CollectionExtensions.GetValueOrDefault<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey). In Microsoft's API reference, it's an extension method declared on this IReadOnlyDictionary<TKey, TValue> dictionary. The compiler infers TKey and TValue from that first argument, my values.

What values is. I had declared it as IDictionary<string, string>. The API reference shows what that interface is built on: IDictionary<TKey,TValue> : ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>. There's no IReadOnlyDictionary<TKey, TValue> in that list, so the compiler couldn't match my argument to the method's first parameter, and had no types to infer.

Why TryGetValue worked. TryGetValue is a method of IDictionary<TKey, TValue> itself, not an extension method, so it needs no inference.

Why It Happens​

GetValueOrDefault isn't a method of every dictionary. It's an extension method written for IReadOnlyDictionary<TKey, TValue>. Dictionary<TKey, TValue> implements both interfaces, so the call works on a Dictionary, but a variable declared as IDictionary<TKey, TValue> only promises the methods of that interface. To the compiler, the call doesn't fit, and it reports the failure as type inference: it found a candidate method but couldn't work out its type arguments from what I passed.

The Fix​

Declare the variable as the interface GetValueOrDefault is written for:

BuildNotifierAgent.cs (BuildFinishedAsync)
var values = turnContext.Activity.Value as IReadOnlyDictionary<string, string> ?? new Dictionary<string, string>();

The fix in a macOS-style editor window: the line that declares values as IDictionary of string and string, then the same line with IReadOnlyDictionary of string and string, followed by the switch that calls values.GetValueOrDefault with status

The rest of the method stays the same: TryGetValue is also a method of IReadOnlyDictionary<TKey, TValue>. The object the SDK passes in is a Dictionary<string, string>, which implements IReadOnlyDictionary<string, string>, so the cast still finds it, and so does the fallback new Dictionary<string, string>().

After the Fix​

Output of dotnet build
Restore complete (0.3s)
BuildNotifier net10.0 succeeded (0.4s) → bin/Debug/net10.0/BuildNotifier.dll

Build succeeded in 1.0s

A macOS-style terminal window: dotnet build of BuildNotifier succeeded, Build succeeded in 1.0s

The agent built and ran, and read both values from the address: a call with ?number=42&status=passed answered ✅ Build 42 passed.

Common Mistakes​

MistakeWhat happensDo this instead
Calling GetValueOrDefault on an IDictionary<TKey, TValue> variableCS0411Declare the variable as IReadOnlyDictionary<TKey, TValue> or Dictionary<TKey, TValue>
Adding type arguments, as the message suggestsThe error becomes CS1929, because the variable still isn't an IReadOnlyDictionary<TKey, TValue>Change the variable's type instead

Troubleshooting​

SymptomLikely causeFix
CS0411 naming GetValueOrDefaultThe variable's declared type doesn't implement IReadOnlyDictionary<TKey, TValue>Change the declared type
The cast gives null after the changeThe object isn't a dictionary of strings at allCheck what the value really is in the debugger

FAQ​

Do I have to change the type, or can I avoid GetValueOrDefault?​

Either works. TryGetValue is a member of IDictionary<TKey, TValue>, so you can read the value with it instead. Changing the declared type keeps the shorter call.

What happens if I add the type arguments, as the message suggests?​

The compiler no longer has to infer them, so it reports the real problem instead. I tried values.GetValueOrDefault<string, string>("status") on the same IDictionary<string, string> variable:

Output of dotnet build (the error line)
error CS1929: 'IDictionary<string, string>' does not contain a definition for 'GetValueOrDefault' and the best extension method overload 'CollectionExtensions.GetValueOrDefault<string, string>(IReadOnlyDictionary<string, string>, string)' requires a receiver of type 'System.Collections.Generic.IReadOnlyDictionary<string, string>'

Same cause, stated plainly: the method needs an IReadOnlyDictionary<TKey, TValue>.

Conclusion​

CS0411 on GetValueOrDefault means you called a dictionary extension method on a variable whose declared type it wasn't written for. GetValueOrDefault extends IReadOnlyDictionary<TKey, TValue>, and IDictionary<TKey, TValue> doesn't inherit it. Declare the variable as IReadOnlyDictionary<TKey, TValue>, or as the Dictionary<TKey, TValue> it really is, and the call compiles.

Additional Resources​