6

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!

Community
  • 1
  • 1
James Ko
  • 32,215
  • 30
  • 128
  • 239
  • 2
    14.0 is the version number of VS2015, not VS2017. So it is still an old one. Use the 2017 Developer Command Prompt and repeat the *where* command to find the new one. – Hans Passant Nov 27 '16 at 07:38

2 Answers2

7

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 4
    "It has been well known that for every new VS release you should use its dedicated prompt with the appropriate environment variables loaded." Having a hard time seeing the value of this remark. – Stevens Miller Aug 06 '17 at 11:55
3

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.

cazam
  • 56
  • 2