Method not found System.Array.Empty

.NET 4.6 introduced a new method, System.Array.Empty which introduces a cache for empty arrays. However this change can cause a problem which commonly shows up in MVC projects during the initialisation of bundles.


[MissingMethodException: Method not found: '!!0[] System.Array.Empty()'.]
MvcApp.BundleConfig.RegisterBundles(BundleCollection bundles) +0
MvcApp.MvcApplication.Application_Start() +384

Strangely enough, this error can often happen even when the project targets 4.5.2 or lower; where this method is not expected to exist! Looking at the IL shows that the build is indeed calling Array.Empty.


IL_0000: ldarg.0
IL_0001: ldstr "~/bundles/jquery"
IL_0006: newobj instance void [System.Web.Optimization]System.Web.Optimization.ScriptBundle::.ctor(string)
IL_000b: ldstr "~/Scripts/jquery-{version}.js"
IL_0010: call !!0[] [mscorlib]System.Array::Empty()
IL_0015: callvirt instance class [System.Web.Optimization]System.Web.Optimization.Bundle [System.Web.Optimization]System.Web.Optimization.Bundle::Include(string, class [System.Web.Optimization]System.Web.Optimization.IItemTransform[])
IL_001a: callvirt instance void [System.Web.Optimization]System.Web.Optimization.BundleCollection::Add(class [System.Web.Optimization]System.Web.Optimization.Bundle)
IL_001f: ldarg.0

So what’s going on here? The problem is that the code is building on a machine without the correct reference assemblies installed. The compiler is linking to the 4.6 reference assemblies instead of; for example, the 4.5.2 assemblies – you’ll find a warning in your build to indicate this. Finding the Array.Empty method; the compiler optimises the call to use this method. At runtime, running against the 4.5.2 assemblies, the method is no longer available and the call fails.

The fix is simple – make sure you’ve got the reference assemblies installed for your target. For example, for 4.5.2 you need to install the 4.5.2 developer pack.

Leave a Reply

Your email address will not be published. Required fields are marked *