Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. enter image description here

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.

like image 339
Jackman Avatar asked Dec 03 '16 07:12

Jackman


1 Answers

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

like image 162
Simon Mattes Avatar answered Oct 04 '22 16:10

Simon Mattes