ValueTuple not working in Razor views with MVC 5

I have an ASP.NET MVC 5 project in Visual Studio 2017 and after successfully adding C# 7 support (which has worked for me so far), I tried using tuples in my Razor views, but I got a surprising error message:

CS0433: The type 'ValueTuple<T1, T2>' exists in both 'System.ValueTuple, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Of course I have installed System.ValueTuple as a nuget package but this error says there's another implementation of that inside mscorlib itself - how does that make any sense? After all, if I remove the nuget package from my project, ValueTuple can't be found anymore. I'm targeting .NET Framework 4.5.2 and using the latest version of all packages that come from ASP.NET.

This problem doesn't even seem to be related to the new C# 7 syntax, it's caused just by trying to use the ValueTuple type:

ValueTuple<int, int> tuple;

I'm even more confused by the error that Visual Studio's IntelliSense gives me, which says that ValueTuple isn't referenced and to use it I have to add a reference to System.Runtime. Installing that helps calm Visual Studio but doesn't make any difference at runtime.

Jon Skeet
people
quotationmark

Of course I have installed System.ValueTuple as a nuget package

I suspect that's the problem. My guess is that even though you're targeting .NET 4.5.2, you're running on a .NET 4.7 system, and that includes System.ValueTuple in mscorlib.

I suggest you remove the package dependency on the System.ValueTuple package, and update your target framework to .NET 4.7 (as you seem to be running on that anyway)... if that change doesn't break other things, of course...

people

See more on this question at Stackoverflow