无法从使用 Visual Studio 2017 和 C# 7.0 的方法返回元组

Unable to return Tuple from a method using Visual Studio 2017 and C# 7.0(无法从使用 Visual Studio 2017 和 C# 7.0 的方法返回元组)
本文介绍了无法从使用 Visual Studio 2017 和 C# 7.0 的方法返回元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了一周前发布的 Visual Studio 2017 Community,并开始探索 C# 7 的新功能.

所以我创建了一个返回两个值的简单方法:

公开课程序{公共静态无效主要(字符串[]参数){(int sum, int count) a = ReturnTwoValues();}static (int sum, int count) ReturnTwoValues() =>(1, 1);}

编译器产生错误:

<块引用>

错误 CS8137 无法定义使用元组的类或成员因为编译器需要类型'System.Runtime.CompilerServices.TupleElementNamesAttribute' 不能成立.您是否缺少参考资料?

我尝试在框架中找到具有此名称的引用,但没有成功!

如果我们需要额外的东西来使用 C# 7.0 的特性,那么我们需要为每个项目都这样做是很奇怪的?!

解决方案

我刚刚在

按照这些步骤,它现在可以工作了.但是,对于我们开始的每个项目都需要这样做,这真的很奇怪!希望当我们正式发布时这个问题得到解决!

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

解决方案

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)

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!

这篇关于无法从使用 Visual Studio 2017 和 C# 7.0 的方法返回元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)