【问题标题】:c# 7 tuples . not able to use tuples in mac dotnet core 1.1c# 7 个元组。无法在 mac dotnet core 1.1 中使用元组
【发布时间】:2017-04-18 03:16:37
【问题描述】:

我正在尝试在 macOS 的 Visual Studio Code 中实现元组 c# 7 新功能。

   using System;
   using System.Collections.Generic;

   namespace newcsharp
   {
    public class Program
    {
        public static void Main(string[] args)
        {
            int[] numbers = { 1, 3, 4, 10, 6, 20, 78, 15, 6 };
            var result = Range(numbers);
            Console.ReadLine();
        }

        private static (int Max, int Min) Range(IEnumerable<int> numbers)
        {
            int min = int.MaxValue;
            int max = int.MinValue;
            foreach (var n in numbers)
            {
                min = (n < min) ? n : min;
                max = (n > max) ? n : max;
            }
            return (max, min);
        }
      }
   }

我收到以下错误。

我将 System.ValueTuple 包用于在我的项目中使用元组功能。

我的项目.json

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.1.0"
    },
    "System.ValueTuple": "4.3.0"
  },
  "frameworks": {
    "netcoreapp1.1": {
      "imports": "dnxcore50"
    }
  },
  "tooling": {
    "defaultNamespace": "newcsharp"
  }
}

任何帮助表示赞赏。

【问题讨论】:

  • 请在此处粘贴代码,而不是屏幕截图。
  • 删除了屏幕截图并添加了代码。
  • 核心是否支持 C# 7 功能?我的印象是它目前没有(无论如何)。
  • 目前 NET Core 应用程序中似乎不支持 System.ValueTuple 命名空间

标签: c# visual-studio-code .net-core c#-7.0


【解决方案1】:

C# 7 的编译器尚未发布。在您的项目和问题中,没有迹象表明您正在使用它的 RC 版本。 ValueTuple 只是对编译器的类型支持。

等到 VS2017(2017 年 3 月 7 日)发布...应该包含 C# 7,并且在同一时间范围内将有一个 .NET Core 版本很可能包含新编译器。

【讨论】:

    【解决方案2】:

    元组等 C# 7 功能现在可以与最新版本的 VSCode 和 C# 扩展一起使用。
    请注意,您确实需要引用 System.ValueTuple。

    只要确保使用 .csproj 而不是 project.json。

    您可以使用以下最小的 .csproj:

    <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
            <OutputType>Exe</OutputType>
            <TargetFramework>netcoreapp1.1</TargetFramework>
        </PropertyGroup>
        <ItemGroup>
            <PackageReference Include="System.ValueTuple" Version="*"/>
        </ItemGroup> 
    </Project>
    

    享受;)

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2017-10-06
      • 2017-10-16
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 2017-04-28
      相关资源
      最近更新 更多