10

Is there any command to get the C# compiler version? The csc command seams has no option to show compiler version.

P.S when I enter csc command in Developer Command Prompt For VS2015 it returns:

Microsoft (R) Visual C# Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.

However I pretty sure my C# compiler is newer than 1.3!

ma.mehralian
  • 1,274
  • 2
  • 13
  • 29
  • 2
    there can be multiple versions of the compiler on your machine. https://stackoverflow.com/questions/19532942/which-version-of-c-sharp-am-i-using and https://stackoverflow.com/questions/22814922/difference-between-c-sharp-compiler-version-and-language-version shed some light – ADyson Dec 04 '17 at 12:17
  • 1
    Just cos the compiler is 1.3 doesnt mean it cant do .l.net 4.5/6 it just means they didnt have to change it that much.. – BugFinder Dec 04 '17 at 12:20
  • Isn't that the case when each dev command prompt has its own set of variables ( including the one with `csc` ) and you can have multiple cscs out there laying on the hard drive? – mrogal.ski Dec 04 '17 at 12:23
  • When I run csc from Developer Command Prompt For VS2015 it reports version 1.3.1.6016. When I run csc from Developer Command Prompt For VS2012 it reports 4.7.2053.0. – PaulF Dec 04 '17 at 12:23
  • @PaulF so it's the same as I've written above, running `csc` on VS2017 it reads 2.4.0.62225 ( at least for me ). – mrogal.ski Dec 04 '17 at 12:25
  • 1
    @m.rogalski: agreed - looking at the path environment variable they are different - too much in there though to find which version is being found first. On my PC there are 19 instances of CSC.EXE - ranging from version 1.0.0.xxx to version 8.0.xx.xxx – PaulF Dec 04 '17 at 12:28
  • @ADyson If I use the `csc /out:My.exe File.cs` command then what is the default version? – ma.mehralian Dec 04 '17 at 12:33
  • Try running "where csc.exe" from the commandline - it will list all occurences found in the current directory & then via the PATH environment variable. It is the first that will be executed. – PaulF Dec 04 '17 at 12:38
  • @PaulF thanks, it reaturns `.....\v4.0.30319\csc.exe` so it means my C# version is 4? – ma.mehralian Dec 04 '17 at 12:42
  • You can go to that folder in Windows explorer & right click to get the properties. Does it only list the one occurrence? Are you running from the same Developer Command prompt? I would have expected this to have displayed a version 4 number. – PaulF Dec 04 '17 at 12:45
  • @PaulF No the other one is `\MSBuild\14.0\Bin\csc.exe`. yes, same prompt. File properties of `\MSBuild\14.0\Bin\csc.exe` shows 1.3.1.60616 !?! Is it possible to find it in command line? – ma.mehralian Dec 04 '17 at 12:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160433/discussion-between-paulf-and-ma-mehralian). – PaulF Dec 04 '17 at 14:01
  • 1
    In most cases C# compiler version is of little usage. The version of MSBuild you use matters much more. The answers below are already great sharing lots of information, and I have two blog posts with more, [history of C# compiler](https://blog.lextudio.com/the-rough-history-of-the-so-many-c-compilers-f3a85500707c) and [history of MSBuild](https://blog.lextudio.com/the-rough-history-of-msbuild-cc72a217fa98). – Lex Li Dec 04 '17 at 15:24

2 Answers2

7

Do keep in mind that you'll have a least 2 versions of csc.exe on your machine. One is shipped as part of the .NET Framework install, like it always was, and follows the version numbering of the framework. Stored in c:\windows\microsoft.net\framework\v4.0.30319, it is retained there for compatibility with System.CodeDom and sgen.exe. Frozen at C# language version 5. Most programmers right now will have version 4.7.x.0 when they have .NET v4.7 on their machine.

The other one got spun-off as part of the Roslyn project and is stored in the MSBuild directory. With a version number reset that start numbering again from 1. You'll run that one when you use the Developer Command Prompt. The most likely reason for the version number reset is their desire to not get locked into the framework release cadence, Roslyn suffered from a very large number of bugs that required intermediate releases to get fixed. A notable problem caused by the decoupling was the addition of the new ValueTuple type in C# v7, required by the much improved tuple support. The compiler shipped before the framework was available and programmers had to fall back to using a Nuget package for a while.

Version number resets don't happen very often. But another good example that everybody knows about is .NETCore, it got reset from 5.0 back to 1.0. I never saw a solid justification for that beyond "avoids confusion", I think it was a move to make it look fresh.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

As a literal answer to the question new-ish [update: roslyn versions above 2, see comment by Cameron MacFarland] versions of csc do have a /version switch:

c:\>csc /version 
2.3.2.62116 (8522b473)

For scripting purposes (if you want to switch on version say) that may be enough, along with testing the %errorlevel% of csc /version to place in a too old bucket.

/version however does NOT appear at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-alphabetically (which seems the newest version) and I can't find anything listing at what version it was added.

UPDATE: I would also be very careful of the version number reported by csc, for example my personal 'default' is in C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn and has version 2.3.2.62116 (and file date 22/9/2017) but I also have C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe with version 4.7.2046.0 (and file date 18/03/2017) (and which doesn't take the /version switch).

So in answer to

Is there any command to get the C# compiler version?

I would say, yes, perhaps, sometimes but I would treat that version with a pinch of salt, Looking at the version stamps I have the version reported is the FIle Version of the csc.exe assembly which appears to come of the version the the 'larger entity' that compiled csc and there are at least incompatible numbering schemes for the Rosyln and 'Traditional' compilers.

tolanj
  • 3,651
  • 16
  • 30