I’m currently rewriting an existing ASP.NET Web API application to ASP.NET Core. Yesterday I blogged about the use of the ActionResult<T> to combine the type safety of typed controllers with the list of out-of-the-box actionresults.
While introducing the ActionResult<T> class everywhere, I stumbled over one use case where I got a compiler error. Here is the specific code:
When you try to compile this code in Visual Studio it fails with the following error message:
CS0029: Cannot implicitly convert type ‘System.Collection.Generic.IList<T> to Microsoft.AspNetCore.Mvc.ActionResult<System.Collection.Generic.IList<T>>.
The reason is because C# doesn't support implicit cast operators on interfaces. Consequently, we need to convert the interface to a concrete type if we want to use it as our type argument for ActionResult<T>.
An easy fix is to wrap the IList in a concrete type as I did in the example below: