CS0411 The type arguments for method … cannot be inferred from the usage

Evil. I really hate vague error messages. Especially when you search goog, bing, stackover and you find dozens of people talking about it, but none of it is similar to your code, and just frustrates you as you search, and keep finding the wrong help.

My head was melting for 2 days on this one. It is especially stupid, because I made some relatively minor changes, and this goshdarn error showed up all over the place, in code that I didn’t touch.

Here is the error showing up in Visual Studio (a dozen times)

CS0411 The type arguments for method 'TypeInference.CreatePropertyColumn <T, TProperty>(RenderTreeBuilder, int, T, int, Expression<Func<T, TProperty> >, int, string, int, bool?, int, bool)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

This was my code:

The red (error) marker was over a function name (OnClick="@AddPerson"). So I wasted an entire day trying to refactor, and move code, and create references and lambdas, and other wrong-answers. Somehow, the compiler kept adding more errors for every change. Which also made no-sense. The problem didn’t seem to be related to this function or its implementation or the way I referenced it.

The change that I made (before these errors showed up), was I refactored a model class, by using a function for IsInactive instead of a property. I also refactored/expanded a few other properties; like I made Company into Companies (a list of companies). Both of these changes were simple, and I should’ve known which corresponding changes were necessary in my code/forms, but I was called-away-from my work before I could complete the rest of my changes. Monday, I got to work, couldn’t remember what I was doing, so I just started with a compile/rebuild. It gave me these vague errors, and couldn’t find any useful hints online. Bad monday, starting at 8:15am.

TLDR / Solution: As I mentioned, IsInactive is now a function. The solution was to put parens after IsInactive(), and the error went away. Likewise, other places in my code, I just had to look around for brown text and put parens after it. It was such a simple answer, but no-one had suggested it online.

Coincidentally, hovering over the code (which was not marked as part of the error) gave me a hint, but I didn’t think it was part of the problem because, duh, it wasn’t marked.

Explanation: The error message was trying to tell me (since I left-off the parens), the compiler thought I wanted a reference to a function instead of the output from the function. So it was trying to make a delegate, which is nonsense and wouldn’t really work, because you can’t display a function reference (delegate) in a grid. Obviously.

Anyway, if you are also being baffled by this obnoxious error, check for flaws just-after the marked code instead of looking at the part which is marked.

About Tim Golisch

I'm a geek. I do geeky things.
This entry was posted in Blazor, Errors, Programming. Bookmark the permalink.

Leave a comment