2

I'm setting up Visual Studio Code to write and learn C#.

I'm not able to find and use the latest version of C# compiler with Visual Studio Code.

Enquiring enquiring my compiler version with the Visual Studio Code terminal command csc -langversion:? brings up the following error message:

csc : The term 'csc' is not recognized as the name of cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


My previous attempts in solving the problem

Having searched intensely, I have found out this:

  • When using Visual Studio, this problem doesn't even occur. Visual Studio apparently contains the necessary .NET SDK and compiler and uses it automatically.
  • I should locate the csc.exe file and set it as an environment variable path.

The only csc.exe files I find are located in the directory: C:\Windows\Microsoft.NET\Framework64\v x.x.xxxxx with the latest being version being 4.0.30319.

All the instructions I could find suggested using this folder to be set as an environment variable path.

I do that. I now try again in Visual Studio Code: csc -langversion:?

Now I get the following message:

Microsoft (R) Visual C# Compiler version 4.8.4084.0 for C# 5 Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

warning CS2008: No source files specified error CS1617: Invalid option '?' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default"

So the csc.exe I was referring and have set as an environment variable path is outdated. I would, however, like to get all the modern features of C# 9.

I tried again to find the latest compiler from the folder "C:\Program Files\dotnet", but I could only find a folder, "Roslyn", which, to my understanding, refers to the newest versions of csc. I couldn't find any csc.exe in these folders.

The solutions?

Now should I find the latest csc.exe supporting C# 9 from somewhere (where?) and set it up as an environmental variable path?

Or am I on the wrong track here?

Or is the only choice that I give up Visual Studio Code completely and switch to Visual Studio (I wouldn’t want to do that). But that can't be the only solution!


Background information

I am a total beginner to the world of coding.

I have installed the latest version of .NET SDK 5.0.302.

I have installed the following extensions in my Visual Studio Code instance:

  • C# (MS)
  • .NET Install Tool for Extension Authors (MS),
  • C# XML Documentation Comments (Keisuke Kato)
  • ilspy-vscode (SharpDevelop Team)
  • MSBuild project tools (tintoy)
  • REST Client (Huachao Mao)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Olli
  • 31
  • 1
  • 5
  • Download and install the latest .NET SDK and then install the C# extension for VS Code. That should be enough. – Shameel Jul 25 '21 at 13:37
  • https://www.meziantou.net/locating-msbuild-on-a-machine.htm – abdusco Jul 27 '21 at 13:04
  • This question could also have been the duplicate target for the now deleted question *[Option "csc -langversion?" is not working in Visual Studio Code](https://stackoverflow.com/questions/71375497)*. As one of the answers to this question hints at why use of the executable `dotnet` could be relevant. – Peter Mortensen Mar 08 '22 at 11:04

3 Answers3

1

You should use the dotnet command in your console to build and run a .NET 5 application.

Try to install the C# extension in your Visual Studio Code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Thank you for answers. As I mentioned in the end, I have already installed C# extension and .NET SDKs. I did however now locate the csc.exe inside the VSC. I found it here: "\.vscode\extensions\ms-dotnettools.csharp-1.23.13\.omnisharp\1.37.12\.msbuild\Current\Bin\Roslyn" After setting it up as an environmental path, it worked! – Olli Jul 25 '21 at 13:43
1

The crucial piece of findings:

Using statement:

#error version

produces detailed information about the compiler in use. It returned the same version number for the compiler as if I wouldn't have defined the Roslyn folder under the .vscode as PATH in the environment variables.

Therefore I think I can safely conclude this:

  • Visual Studio Code doesn't show you the current compiler with the call csc -langversion:? unless you add the aforementioned Roslyn folder under .vscode as a PATH on environmental variables.
  • However, it does seem that Visual Studio Code uses this particular compiler anyway, even though it doesn't formally recognise it. So the only thing that is really important is to install the latest .NET SDK.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Olli
  • 31
  • 1
  • 5
0

I did the following and it worked.

I went to My ComputerPropertiesAdvanced System SettingsEnvironment Variables → double clicked on "Path" → Add new → pasted the directory where you can find csc.exe and clicked OK.

I restarted Visual Studio Code and it worked. The directory where I found csc.exe for me was:

C:\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.net.native.compiler\2.2.10-rel-29722-00\tools\csc

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zeus Kaka
  • 7
  • 1
  • 5