I've installed Visual Studio 15 Preview 3 and tried to use the new tuple feature
static void Main(string[] args)
{
var x = DoSomething();
Console.WriteLine(x.x);
}
static (int x, int y) DoSomething()
{
return (1, 2);
}
When I compile I get the error:
Predefined type 'System.ValueTuple´2´ is not defined or imported
According to the blog post, this features should be "on" by default.
What did I do wrong?
This question is related to
c#
visual-studio-2017
c#-7.0
We were seeing this same issue in one of our old projects that was targeting Framework 4.5.2. I tried several scenarios including all of the ones listed above: target 4.6.1, add System.ValueTuple package, delete bin, obj, and .vs folders. No dice. Repeat the same process for 4.7.2. Then tried removing the System.ValueTuple package since I was targeting 4.7.2 as one commenter suggested. Still nothing. Checked csproj file reference path. Looks right. Even dropped back down to 4.5.2 and installing the package again. All this with several VS restarts and deleting the same folders several times. Literally nothing worked.
I had to refactor to use a struct instead. I hope others don't continue to run into this issue in the future but thought this might be helpful if you end up as stumped up as we were.
The ValueTuple types are built into newer frameworks:
Until you target one of those newer framework versions, you need to reference the ValueTuple package.
More details at http://blog.monstuff.com/archives/2017/03/valuetuple-availability.html
In case others have the same problem, I ran into this error after updating a project to 4.7. Oddly enough, I had to remove the System.ValueTuple reference for this error to go away.
I would not advise adding ValueTuple
as a package reference to the .net Framework projects. As you know this assembly is available from 4.7 .NET Framework.
There can be certain situations when your project will try to include at all costs ValueTuple
from .NET Framework folder instead of package folder and it can cause some assembly not found errors.
We had this problem today in company. We had solution with 2 projects (I oversimplify that) :
Lib
Web
Lib
was including ValueTuple and Web
was using Lib
. It turned out that by some unknown reason Web
when trying to resolve path to ValueTuple
was having HintPath
into .NET Framework directory and was taking incorrect version. Our application was crashing because of that. ValueTuple
was not defined in .csproj
of Web
nor HintPath
for that assembly. The problem was very weird. Normally it would copy the assembly from package folder. This time was not normal.
For me it is always risk to add System.*
package references. They are often like time-bomb. They are fine at start and they can explode in your face in the worst moment. My rule of thumb: Do not use System.*
Nuget package for .NET Framework if there is no real need for them.
We resolved our problem by adding manually ValueTuple
into .csproj
file inside Web
project.
It's part of the .NET Framework 4.7
.
As long as you don't target the above framework or higher (or .NET Core 2.0
/ .NET Standard 2.0
), you'll need to reference ValueTuple
. Do this by adding the System.ValueTuple
NuGet Package
I had to check System.ValueTuple.dll file was under source control and correct its reference in .cssproj files:
< Reference Include="System.ValueTuple" >
< HintPath >
....\ProjectName\ProjectName\obj\Release\Package\PackageTmp\bin\System.ValueTuple.dll
< /HintPath >
< /Reference >
into
< Reference Include="System.ValueTuple" >
< HintPath >
..\packages\System.ValueTuple.4.4.0\lib\netstandard1.0\System.ValueTuple.dll
< /HintPath >
< /Reference >
(Optional): 7. solve same problems with another .dll files this way
I also came accross this issue as I upgraded from .NET 4.6.2 to .NET 4.7.2. Unfortunately, I was not able to remove the package reference to System.ValueTuple
because another NuGet package I use depends on it.
Finally I was able to locate the root cause: There was a .NET 4.6.2 version of mscorlib.dll
lying around in the project folder (output of a publish operation) and MSBuild decided to reference this assembly instead of the official .NET 4.7.2 reference assembly located in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2
.
Due to the fact that System.ValueTuple was introduced in .NET 4.7, MSBuild failed the compilation because it could not find the type in the reference assembly of .NET 4.6.2.
(duplicate of https://stackoverflow.com/a/57777123/128709)
Make sure you have .NET 4.6.2 Developer Pack for VS installed and then pull in System.ValueTuple
package from NuGet.
For Visual Studio Code use the built in Terminal and run:
dotnet add package "System.ValueTuple"
Don't forget to run dotnet restore
afterwards.
Source: Stackoverflow.com