Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the command-line csc compile C# 7 after installing VS 2017?

I just downloaded Visual Studio 2017 RC, which was released a few days ago and comes with support for C# 7. I can use C# 7 features from the IDE:

However, this behavior does not seem to hold for the command-line. I am working on a project that requires the csc executable to handle C# 7 and higher. However, when I try changing to the same directory as the project and compiling the file, I get

> csc Program.cs /target:exe
Microsoft (R) Visual C# Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(12,23): error CS1026: ) expected
Program.cs(12,25): error CS1001: Identifier expected
Program.cs(12,25): error CS1002: ; expected
Program.cs(12,26): error CS1002: ; expected
Program.cs(12,26): error CS1513: } expected
Program.cs(13,32): error CS1003: Syntax error, '=>' expected
Program.cs(13,32): error CS1525: Invalid expression term '='

So clearly, it looks like the version of csc found in my PATH does not support C# 7. I did a little research on this and found a similar question for C# 6, which suggested checking to make sure you are invoking the csc from %PROGRAMFILES(x86)%\MSBuild\14.0\Bin instead of the old one from C:\Windows\Microsoft.NET\Framework\4.0.30319, since the latter only supports C# 5. So I did just that:

> where csc
C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

As you can see the one from the MSBuild\14.0\Bin directory gets chosen, not the old one in v4.0.30319. I additionally ran csc /version which tells me that the version of csc is 1.3.1.60616, which indeed only supports C# 6.

Does anyone have a clue how to enable C# 7 features for the version of csc on the command line? Thanks!

like image 227
James Ko Avatar asked Nov 27 '16 01:11

James Ko


2 Answers

You have to launch "Developer Command Prompt for VS 2017 RC". Then you can see that csc.exe will have a version number of 2.0.

It has been well known that for every new VS release you should use its dedicated prompt with the appropriate environment variables loaded.

like image 113
Lex Li Avatar answered Nov 01 '22 21:11

Lex Li


You can run "where csc.exe" from the visual studio 2017 command prompt and export the version of csc you need by adding it to the Path system variable. I was in a similar case where i needed to compile via command line and i ended up setting

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\

in system Path.

like image 45
cazam Avatar answered Nov 01 '22 21:11

cazam