113

I've installed Visual Studio 2017 Community that was released a week ago, and I started exploring the new features of C# 7.

So I created a simple method that returns two values:

public class Program
{
    public static void Main(string[] args)
    {
        (int sum, int count) a = ReturnTwoValues();
    }

    static (int sum, int count) ReturnTwoValues() => (1, 1);
}

Compiler is generating an error:

Error CS8137 Cannot define a class or member that utilizes tuples because the compiler required type 'System.Runtime.CompilerServices.TupleElementNamesAttribute' cannot be found. Are you missing a reference?

I tried finding a reference in the framework with this name, but with no luck !

If we need additional stuff to use C# 7.0 features, then it is very weird that we need to do that for every project ?!

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Possible duplicate of [Enabling c# 7 in a asp.net application](https://stackoverflow.com/questions/42744689/enabling-c-sharp-7-in-a-asp-net-application) – Hassan Abdullah Jul 04 '17 at 10:22

4 Answers4

138

I Just ran through this page on Roslyn which describes the following steps to get this working:

  1. Start a C# project
  2. Add a reference to the System.ValueTuple package from NuGet (pre-release)

enter image description here

Following those steps, it is now working. But it is really very weird that we need to do that for every single project that we start! Hope this is fixed when we reach the Official release!

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • 2
    One thing that should be in the next release is a code fix that lets you easily add the package. – svick Nov 27 '16 at 12:19
  • 7
    This solution helped me; even with the full release version of VS2017 Professional, downloaded in March 2017! – Nij Mar 09 '17 at 23:27
  • This does get the ball rolling using tuples – Leo Gurdian Mar 14 '17 at 23:34
  • 2
    Very annoyed that this isn't in the official language. Ran into the same problem with this being the fix. They are marketing this as a C# 7 language feature when in fact it's more of a C# 7 extension feature. I guess things can get hairy like that when you're using the new Roslyn compiler (which I'm a fan of)... either way, this is a sad side effect IMO. – Michael Puckett II Mar 15 '17 at 21:29
  • 10
    @MichaelPuckettII I think this is because this feature needs classe(s) to exist in the .NET framework and they haven't released a new one yet, that's why a temporary solution was to include this as a Nuget Package. When the next .NET framework is released, the classes needed for this feature should be already built-in. If not, then that is a problem that needs some explanation. – Zein Makki Mar 16 '17 at 07:16
  • 17
    After just upgrading to .NET Framework 4.7 I received this error, turns out ValueTuple is now part of 4.7, so uninstalling the tuple package was the solution for me – chad.mellor Apr 16 '17 at 05:16
  • Still using .NET Framework 4.6 so this worked for me. – Spencer Dec 05 '17 at 23:39
74

I started getting this error after I installed .Net 4.7 Framework, and changed my project to target .Net 4.7

ValueTuple is now included with .Net 4.7, so you don't have to reference the ValueTuple manually.

All I had to do to correct the compile error was remove the reference to System.ValueTuple from my project's references.

aaaa bbbb
  • 3,003
  • 2
  • 23
  • 23
11

I got this error too after updating to .NET 4.7.2 and was able to fix it by re-installing nuget packages using:

Update-Package -Reinstall
martinoss
  • 5,268
  • 2
  • 45
  • 53
5

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.

Oliver Hanappi
  • 12,046
  • 7
  • 51
  • 68