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

编程入门 行业动态 更新时间:2024-10-28 02:28:02
本文介绍了无法从使用 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:

  • Start a C# project
  • 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 的方法返回元组

    本文发布于:2023-11-04 10:39:04,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1557691.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:方法   Studio   Visual

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!