Unable to return Tuple from a method using Visual Studio 2017 and C# 7.0

Zein Makki picture Zein Makki · Nov 27, 2016 · Viewed 22.7k times · Source

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 ?!

Answer

Zein Makki picture Zein Makki · Nov 27, 2016

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!