c# 7 tuples . not able to use tuples in mac dotnet core 1.1

I am trying to implement tuples c# 7 new feature in Visual Studio Code in macOS.

   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);
        }
      }
   }

I am getting the following erros. 在这里输入图像描述

I included System.ValueTuple package for using tuples feature in my project.

My project.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"
  }
}

any help appreciated.


The compiler for C# 7 is not yet released. In your project and question there is no indication you are using a RC version of it. The ValueTuple is just type support for the compiler.

Wait till the release of VS2017 (07 MAR 2017)... that should contain C# 7 and in the same timeframe there will be a .NET Core release most probably containing the new compiler.


C# 7 features such as tuples now work natively with the latest version of VSCode and the C# extension.
Note that you do need to reference System.ValueTuple.

Just make sure to use .csproj instead of project.json.

You can use the following minimal .csproj:

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

Enjoy ;)

链接地址: http://www.djcxy.com/p/37334.html

上一篇: 从不同的仓库导入组件

下一篇: c#7元组。 无法在mac dotnet core 1.1中使用元组