Roslyn: HOWTO use C# 7 with .NET Core

Created on 25 Aug 2016  路  15Comments  路  Source: dotnet/roslyn

Is there a HOWTO somewhere on using C# 7 with .NET Core projects, maybe even with VS Code on Linux?

(Sorry, I would have asked on Gitter but it's blocked from my work network.)

Area-Compilers Concept-Portability Documentation Investigation Required Question Resolution-Answered

Most helpful comment

I've managed to figure out a way, but it's super convoluted (possibly unnecessarily).

I would suggest that your ask on dotnet/cli to make this easier, but with the upcoming move to msbuild, everything is likely going to change anyway, so there wouldn't be much point in doing that.

My approach (using NuGet Package Explorer for steps 1 and 2):

  1. Extract runtimes/any/native/csc.exe from Microsoft.Net.Compilers.netcore (in my case, version 2.0.0-beta5-60827-02) from the roslyn-master-nightly MyGet feed.
  2. Create a package with:

    • dependency on the right version of Microsoft.CodeAnalysis.CSharp in the .NETCoreApp10 group

    • the file from step 1 at lib/netcoreapp1.0/csc.dll.dll (not a typo)

    • the following text at lib/netcoreapp1.0/csc.dll.runtimeconfig.json:

 ```
 {
   "runtimeOptions": {
     "framework": {
       "name": "Microsoft.NETCore.App",
       "version": "1.0.0"
     }
   }
 }
 ```

I named the package csc, with the same version as Microsoft.Net.Compilers.netcore. You should be able to download the finished package here.

  1. Put the csc package into some directory, in my case C:\package-source.
  2. Switch to the directory of your .Net Core application, and create NuGet.Config file with the following content there:

<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="local" value="C:\package-source" /> <add key="roslyn-master-nightly" value="https://dotnet.myget.org/F/roslyn-master-nightly/api/v3/index.json" /> <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" /> </packageSources> </configuration>

Adjust the path to the directory with the csc package accordingly.

  1. Add the following to your project.json:

"tools": { "csc": "2.0.0-*" },

  1. Run dotnet restore and then enjoy using pre-release version of C# 7.0 in your .Net Core application.

This approach works for me with the dotnet CLI on Windows 10 and Ubuntu 16.04.

All 15 comments

I've managed to figure out a way, but it's super convoluted (possibly unnecessarily).

I would suggest that your ask on dotnet/cli to make this easier, but with the upcoming move to msbuild, everything is likely going to change anyway, so there wouldn't be much point in doing that.

My approach (using NuGet Package Explorer for steps 1 and 2):

  1. Extract runtimes/any/native/csc.exe from Microsoft.Net.Compilers.netcore (in my case, version 2.0.0-beta5-60827-02) from the roslyn-master-nightly MyGet feed.
  2. Create a package with:

    • dependency on the right version of Microsoft.CodeAnalysis.CSharp in the .NETCoreApp10 group

    • the file from step 1 at lib/netcoreapp1.0/csc.dll.dll (not a typo)

    • the following text at lib/netcoreapp1.0/csc.dll.runtimeconfig.json:

 ```
 {
   "runtimeOptions": {
     "framework": {
       "name": "Microsoft.NETCore.App",
       "version": "1.0.0"
     }
   }
 }
 ```

I named the package csc, with the same version as Microsoft.Net.Compilers.netcore. You should be able to download the finished package here.

  1. Put the csc package into some directory, in my case C:\package-source.
  2. Switch to the directory of your .Net Core application, and create NuGet.Config file with the following content there:

<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="local" value="C:\package-source" /> <add key="roslyn-master-nightly" value="https://dotnet.myget.org/F/roslyn-master-nightly/api/v3/index.json" /> <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" /> </packageSources> </configuration>

Adjust the path to the directory with the csc package accordingly.

  1. Add the following to your project.json:

"tools": { "csc": "2.0.0-*" },

  1. Run dotnet restore and then enjoy using pre-release version of C# 7.0 in your .Net Core application.

This approach works for me with the dotnet CLI on Windows 10 and Ubuntu 16.04.

@piotrpMSFT

@svick nice! why does it need the double .dll extension?

@tmds It's necessary to trick the CLI into resolving csc from a tool package instead of using the one installed with the dotnet SDK. (It's looking for a command named csc.dll, and while commands from packages aren't named with extension, SDK commands are.)

@svick that sounds good, but... unfortunately I get

error CS0041: Unexpected error writing debug information -- 'Windows PDB writer is not available -- could not find Microsoft.DiaSymReader.Native.amd64.dll'

when trying to compile a new netcoreapp1.0 console application
.NET Command Line Tools (1.0.0-preview2-003131)
Windows 8.1

any suggestions?

Is there an update on how this works now that C# 7 is released and .net core has switched over to xml from json config files?

@ochowie As far as I can tell, .Net Core SDK 1.0.0 supports C# 7.0 out of the box, you don't have to do anything to start using it.

I think that means this issue can be closed.

OK. So looks like you're correct. I was trying to make record types work but it looks like they've been removed from the C# 7 release. The only thing I see is that I have to include another dependency to work with Tuples (System.ValueTuple). Is this the permanent state for .NET core or will it get integrated in a future release. If necessary, I can open a separate issue for that.

@ochowie this is not an issue, but by design. Although I also feel that having to import the System.ValueTuple to have language support for tuple is silly. I'm sure eventually it will become integrated in some future version of .NET fwk / standard

@svick many thanks for your solution, which allowed me to use .NET Core an C# 7 until now (but I'm glad this is no longer needed :p )

@ochowie

The only thing I see is that I have to include another dependency to work with Tuples (System.ValueTuple). Is this the permanent state for .NET core or will it get integrated in a future release.

If I'm reading https://github.com/dotnet/roslyn/issues/13177 right, ValueTuple will be added to the core library in .Net Framework 4.7.1 and .Net Core 2.0.

Should we close this issue now?

So basically in order to use Tuples with .NET Core you have to install the System.ValueTuple package from nuget.

I'll go ahead and close the issue as Answered.

As we start releasing minor versions of C#, such problems are going to become more common. I am tracking a list of issues to smooth this adoption process (more details in https://github.com/dotnet/roslyn/issues/18783).

You can add this to the .csproj

  <PropertyGroup>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
Was this page helpful?
0 / 5 - 0 ratings