Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 7 ValueTuple compile error

Tags:

c#

c#-7.0

I'm using VS2017 RC and my application targets net framework 4.6.1.

I have two assemblies referencing System.ValueTuple 4.3

MyProject.Services MyProject.WebApi

In MyProject.Services I have a class with a method like this

public async Task<(int fCount, int cCount, int aCount)> GetAllStatsAsync() {     // Some code...     return (fCount, cCount, aCount); } 

In MyProject.WebApi I have a controller that use this method like that:

public async Task<HttpResponseMessage> GetInfoAsync() {     // Some code...     var stats = await _myClass.GetAllStatsAsync();      var vm = new ViewModel              {                  FCount = stats.fCount,                  CCount = stats.cCount,                  ACount = stats.aCount              };       return Request.CreateResponse(HttpStatusCode.OK, vm); } 

Intellisense is working and deconstruct the tuple but when I compile it fails without any Error in Error List window. In the output windows I have this errors:

2>MyController.cs(83,31,83,40): error CS1061: 'ValueTuple' does not contain a definition for 'fCount' and no extension method 'fCount' accepting a first argument of type 'ValueTuple' could be found (are you missing a using directive or an assembly reference?) 2>MyController.cs(84,39,84,49): error CS1061: 'ValueTuple' does not contain a definition for 'cCount' and no extension method 'cCount' accepting a first argument of type 'ValueTuple' could be found (are you missing a using directive or an assembly reference?) 2>MyController.cs(85,35,85,40): error CS1061: 'ValueTuple' does not contain a definition for 'aCount' and no extension method 'aCount' accepting a first argument of type 'ValueTuple' could be found (are you missing a using directive or an assembly reference?)

I tried adding the DEMO and DEMO_EXPERIMENTAL build flags but still fails.

Any idea on what's wrong?

EDIT 1:

This code works and stats is well deconstructed. I'm probably hitting a bug.

public async Task<HttpResponseMessage> GetInfoAsync() {     // Some code...     var stats = await _myClass.GetAllStatsAsync();     var tu = stats.ToTuple();     var vm = new ViewModel              {                  FCount = tu.Item1,                  CCount = tu.Item2,                  ACount = tu.Item3              };       return Request.CreateResponse(HttpStatusCode.OK, vm); } 

EDIT 2:

Issue open on github here: https://github.com/dotnet/roslyn/issues/16200

like image 825
JuChom Avatar asked Jan 03 '17 13:01

JuChom


People also ask

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in coding language?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

If anyone falls in the same trap, to fix this you need to update this package: Microsoft.Net.Compilers to 2.0 (you need to show pre-release)

like image 174
JuChom Avatar answered Oct 03 '22 02:10

JuChom


I just want to add to the other answers.

Remove System.valuetuple from references. Otherwise, it doesn't work and I do not know why. Basically, value tuple is already in 4.7.2 and so if you use visual studio 2019 you're all set.

If you use visual studio 2019 and I did, that's what worked for me. I don't exactly know why. No need for nugget. No need to reference that directly.

enter image description here

Target your project to use .net framework 4.7.2 and remove references to valuetuple.

like image 28
user4951 Avatar answered Oct 03 '22 00:10

user4951