Wednesday, April 19, 2017

Value Tuples in C# 7: Please Tell Me I'm Doing This Wrong

I really need someone to tell me that I'm doing this wrong. Because what I found is that I need to install a NuGet package in order to use a C# language feature.

The Discovery
I was doing some experimentation last night, and I decided that I would try to use value tuples, a new feature in C# 7. (Long story short, I was trying to implement a Fibonacci sequence in C# using a more declarative approach (similar to what I did in F# last year).)

So I fired up Visual Studio 2017, got a working implementation going with TDD (I'll write about that in a future article), then I went to refactor it to use a value tuple.

As a first step, I created a local variable:


That's when I ran into a problem. Red squigglies? I double checked the docs online to make sure I was using the right syntax. Yep.

Here's the build error:


Great. Now what? "System.ValueTuple" doesn't show up in my available assembly references (and yet, somehow the compiler knows about it).

Looking at the Microsoft docs (https://docs.microsoft.com/en-us/dotnet/articles/csharp/whats-new/csharp-7#tuples), I came across this note:


But I'm not using a Preview. I have the release version of Visual Studio 2017 (Version 15.1 (26403.7) Release). Surely I don't need a NuGet package.

But the only way I could get this to work was to install the NuGet package:


After installing this, my tuple started working fine:


The Environment
  • Visual Studio 2017 Enterprise (Version 15.1 Release)
  • .NET Version 4.6.2
  • Windows 10 (who knows what actual version, not the Creator's Update)
Please, Please, Please
I really need someone to tell me that I'm doing this wrong. Do I really need to install a NuGet package to use a language feature?

If I need to install a package, is it really a language feature? Or is it just another library.

[Update: The package is not needed with .NET 4.7. But as of today, .NET 4.7 (and the ability to target it in Visual Studio) is *only* available for Windows 10 Creator's Update.]

I am not connected to the internet 24 hours a day. And when I am, I don't always have a high speed connection. I already find myself going back to Visual Studio 2015 periodically just because I have local help installed there.

Visual Studio has been a really awesome development environment. But I find it harder and harder to develop with Visual Studio 2017.

So, please tell me that I'm doing something wrong. I want my peace of mind back.

2 comments:

  1. .Net Framework 4.7 brings ValueTuple baked in and removes the need for the package.

    ReplyDelete
  2. It's not the first language feature that requires framework support... It was the same for Linq, extension methods, dynamic, async/await, etc. Tuples rely on a new type, and this type has to come from somewhere. It is included in 4.7, but not in earlier versions. For earlier versions you need the NuGet package. It's not such a big deal... The real problem is that 4.7 was released *after* VS2017. I think they should have waited and released VS2017 and .NET 4.7 together.

    ReplyDelete