C# 7 ValueTuple compile error

前端 未结 3 1917
天命终不由人
天命终不由人 2020-12-06 04:01

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.Web

相关标签:
3条回答
  • 2020-12-06 04:31

    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)

    0 讨论(0)
  • 2020-12-06 04:35

    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.

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

    0 讨论(0)
  • 2020-12-06 04:41

    I think It is because you've not defined fCount, cCount and aCount. Try this

    public async Task<(int fCount, int cCount, int aCount)> GetAllStatsAsync()
    {
        // Some code...
        //fCount, cCount, aCount are not defined here, so you should define them
    
        var fCount = 0;
        var cCount= 0;
        var aCount = 0;
        return (fCount , cCount, aCount );
    
        //Other ways:
        //return (fCount : 0, cCount: 0, aCount : 0);
        //return new (int fCount , int cCount, int aCount ) { fCount = 0, cCount = 0, aCount = 0 };
    }
    
    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);
    }
    

    Edited with @Swell suggestion

    Take a look at this post

    0 讨论(0)
提交回复
热议问题