I have been trying to update my C# compiler after getting the below error when trying to compile a file:
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
I downloaded Roslyn as directed (not sure if it needed to go in a particular folder with the visual studio files) but didn't get any further with it.
I also found guidance to run the below command in visual studio :
Install-Package Microsoft.Net.Compilers
But this also generates errors when attempting, regardless of whether or not a solution is open.
I'm trying to get the compiler to update to version 6, I feel like this should be very simple but I haven't gotten anywhere with the advice I've researched. Any ideas?
If you are using Visual Studio 2015 Community Edition you should already have the C# 6 compilers since they shipped with Visual Studio 2015.
I just installed the RTM version of Visual Studio 2015 Community Edition on a clean system. The csc.exe compiler it installs (under C:\Program Files (x86)\MSBuild\14.0\Bin\ reports itself as Microsoft (R) Visual C# Compiler version 1.0.0.50618. The file's modified date is 6/21/2015.
The help section on the langversion switch reports /langversion: <string> Specify language version mode: ISO-1, ISO-2, 3, 4, 5, 6, or Default
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
The wording of this warning message suggests that you are using a version of the C# compiler shipped with the .NET Framework rather than one that was shipped with Visual Studio 2015.
Related
I would like to write some console apps using C# 11. I know that it can be only with .NET 7x. If I write
<TargetFramework>netcoreapp7</TargetFramework>
in my .csproj file, I will get NETSDK1045 instead of running. But "file" (type modifier) is useful.
As written in the .NET 7 release notes for .NET 7 support in Visual Studio you need to install 17.4+ version:
You need Visual Studio 17.4 latest preview to use .NET 7.0 on Windows. On macOS, you need the latest version of Visual Studio for Mac. The C# extension for Visual Studio Code supports .NET 7.0 and C# 11.
You can try using 2019 version but some features even for .NET 6 (see this answer) will not work correctly (note that building and running from command line via dotnet ... still should work correctly if appropriate SDK is installed).
So in short - install the latest VS 2022.
There's no inherent downside to upgrading your Visual Studio (as long as your tools are compatible). Within it you can still develop for older versions.
As mentioned by rbdeenk, you have to upgrade Visual Studio as per this link: https://dotnet.microsoft.com/en-us/download/dotnet/7.0
I started getting error:
Error NETSDK1045: The current .NET SDK does not support targeting .NET 6.0
Visual Studio Version:
I have followed lot of posts about:
Updating Visual studio >> I am using latest
updating .net >> done
no MSBuildPath env variable
no global.json set
none of the solutions worked.
'dotnet info outout'
Any help
Referring to Announcing .NET 6:
.NET 6 is supported with Visual Studio 2022 and Visual Studio 2022 for Mac. It is not supported with Visual Studio 2019, Visual Studio for Mac 8, or MSBuild 16. If you want to use .NET 6, you will need to upgrade to Visual Studio 2022 (which is also now 64-bit).
If you want to use .NET 6 you will need to update to Visual Studio 2022, as it is not supported in Visual Studio 2019.
In this Developer Community issue, Microsoft have stated:
We have no plans to support .NET 6 in earlier versions of VS. The general reason is that we add features to VS in order to support newer features in the runtime, languages, and libraries. It’s not practical to backport those.
I wrote some code in .NET Project on a windows 10 machine with visual studio 2017.
The target framework ist 4.5.2. and the used C# version is default (running the csc tool under "C:\Windows\Microsoft.NET\Framework\v4.0.30319" shows C# 5.
I have used a feature from C# 6. The Monadic null checking (https://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated).
and the compiler doesn't complain about it.
Is that a normal bhaviour or am I missing something? Shouldn't the compiler complain about it?
What you're missing is that the C# compiler being used isn't the csc.exe you found. Visual Studio comes with its own C# compiler which supports C# 6 (and later, depending on the exact version of VS you've got installed).
C# language version is decided by Visual Studio, not by .Net Framework.
As long as your Visual Studio version supports the C# version you will not face any error.
Configuring Language version in Visual Studio: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version
I installed StyleCop 4.7 for developing customized rules, to analyze our C# code.
We write the code in Visual Studio 2015. I can analyze my C# 5.0 (Visual Studio 2013) code, and I get correct response, but when I analyze my C# 6.0 (Visual Studio 2015) code, StyleCop raises an exception for my rules.
Now my question is:
Is StyleCop 4.7 compatible with C# 6.0 code? If not, do we have any solution?
TL;DR - Yes. StyleCop 4.7 was updated to be compatible with C# 6 starting with 4.7.51 (Beta) released on 13th March 2016.
There was a handover to new custodians back in January 2016, and a new version 4.7.50 alpha has been released 3rd Feb 2016, but as yet does not include C# 6 support.
4.7.51 (Beta) was released on 13th March 2016 and the latest stable is 4.7.54 released May 13th.
Here are three other options that work with Visual Studio 2015:
1. Visual StyleCop
Visual StyleCop, is an extension to Visual Studio. This gives you the right click functionality into Visual Studio 2015 that installing StyleCop gave you for 2013 and below.
This is available by adding an extension into Visual Studio:
It is an active project and will give you StyleCop on C# 6 features. There is a package - Visual-StyleCop.MSBuild, that will give you MSBuild integration.
It is possible to write custom rules with Visual StyleCop.
2. StyleCop Analyzers
StyleCop.Analyzers are built upon Rosalyn and make use of the Analyzers feature of Visual Studio 2015. They are available on NuGet:
This would add them into the rules in the relevant *.ruleset file (same place as CodeAnalysis rules, accessible from the project page:)
giving:
and you can run them via
Which has the same effect as right click, Run StyleCop, used on previous versions and with Visual StyleCop.
Giving:
The source code is available on GitHub, so adding custom rules should be possible.
3. Resharper, with StyleCop by Jetbrains
Install:
Resharper 10.0.1
StyleCop by JetBrains 4.8
Which gives this in R# options:
This gives you syntax highlighting, but no right click run, and no build integration. Additionally, R# is not a free product.
You can extend it using plugins, but it doesn't feel as customizable as the other two options presented.
Version 4.7.51 and later should support C# 6:
Added initial (and untested) support for C# 6.0 syntax
http://stylecop.codeplex.com/releases/view/620339
So the current stable build should support it, too: http://stylecop.codeplex.com/releases
There was time I thought that the Framework version and the C# version are the same things, so once you install the next Framework version on the computer, you should use it.
Then I found out that the framework is not linked directly with the C# version, and on the same machine multiple C# compilers can be cohabitate, so probably the compiler and C# version should be the same.
Now I understand that the compiler version and the C# version are not the same...
Visual Studio Command Prompt (2010): C:\>csc
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Developer Command Prompt for VS 2013: C:\>csc
Microsoft (R) Visual C# Compiler version 12.0.30110.0
for C# 5
we can see that
- VS 2010 uses a compiler version 4.0 for the C#4 (?? I just can suppose it, because not explicitly mentioned);
- VS 2013 uses the compiler version 12.0 fo the C# 5 (this is explicitly mentioned)
Knowing that compiling using different language versions could bring different results to the user
Questions
How to find out what C# version (not the compiler one, but the language one) uses VS to build my concrete project?
Is there a strict, clear and transparent link between the C# compiler and language versions?
Can I indicate to Visual Studio (in case of migration issues from one Studio version to another) to use different compiler version for my concrete solution?
As nobody gives a good enough answer, I will have a try now.
First, C# has its version history published by Microsoft now (coming from MVP posts obviously),
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history
So you can easily see what new features are added for each new releases.
Second, we will talk about the compiler releases, which initially were part of .NET Framework.
Below I list a few milestones (might not be 100% correct, and some versions might be skipped),
csc.exe 1.0 (?) for .NET Framework 1.0 (implements C# 1.0).
csc.exe 2.0 (Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.8745) for .NET Framework 2.0 (implements C# 2.0, as well as 1.0 for compatibility).
csc.exe 3.5 (Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.8763) for .NET Framework 3.5 (implements C# 3.0, and older versions).
csc.exe 4.0 (?) for .NET Framework 4.0 (implements C# 4.0, and older).
csc.exe 4.x (like Microsoft (R) Visual C# Compiler version 4.7.2053.0) for .NET Framework 4.5 and above (implements C# 5.0, and older). Note that the version numbers vary a lot (from 4.x to 12.x) based on the .NET Framework on your machine (4.5.0 to 4.7.1).
Then Microsoft made the old csc.exe obsolete (as they were native executable), and shipped Roslyn based compiler instead (though still csc.exe). In the meantime, C# compiler is no longer part of .NET Framework, but part of VS.
It was the same time, that C# compiler, language version, and .NET Framework are fully decoupled, so that you can easily use multi-targeting.
Roslyn csc.exe 1.x (?) implements C# 6.0 and older. Shipped with VS2015.
Roslyn csc.exe 2.x (like Microsoft (R) Visual C# Compiler version 2.4.0.62122 (ab56a4a6)) implements C# 7.x and older. Shipped with VS2017.
Ok, enough background. Back to your questions.
Q1: How to find out what C# version (not the compiler one, but the language one) uses VS to build my concrete project?
Answer: You can easily see from project settings that what language version is used.
If you don't choose an explicit version, it can automatically use the latest version supported by the csc.exe compiling the project.
Note that #Servy commented under #DaniloCataldo's answer about the langversion switch with more details. That switch has its design goals and limitation. So for example even if you force Roslyn 2.x compiler to compile your project based on C# 4.0, the compiled result would be different from what C# 4.0 compiler does.
Q2: Is there a strict, clear and transparent link between the C# compiler and language versions?
Answer: Please refer to the background I described above, I think that already answered this part. There is a strict, clear and transparent link.
Q3: Can I indicate to Visual Studio (in case of migration issues from one Studio version to another) to use different compiler version for my concrete solution?
Answer: A Visual Studio release (like VS2019) sticks to an MSBuild release (16.x), so a dedicate version of C# compiler. So in general Q3 is duplicate to Q1, as you can only change language version.
There are a bunch of NuGet packages to override C# compiler used by a project, such as https://www.nuget.org/packages/Microsoft.Net.Compilers.Toolset. However, Microsoft states that "Using it as a long term solution for providing newer compilers on older MSBuild installations is explicitly not supported", so you really shouldn't explore that route.
In the past Visual Studio 2005 was fixed only to one .Net version and C# compiler delivered with this version. In case you want use newer version of VS you have to switch Visual Studio as well. Now Visual studio can target to more than one .Net version and it can even mix new C# compiler with old .Net framework (lambdas or extension methods in .Net 2.0). Simply C# compiler version is related to C# language version.
You can check your compiler version in project file (open it as xml) and there is ToolsVersion attribute of Project element.
In my specific project there is ToolsVersion="4.0" and my target project is .Net 2.0. It means I can use new language construct in old framework which is not possible in VS2005.
Just to add to the previous answer.
You should be aware that while the C# language and the C# compiler are separate from .Net framework, they still depend on it.
For example, the C# 5 has await/async language feature, but you can't use it with the .Net 4, at least not without some extra actions and nuget packages.
On the other hand, you still can use the nameof or null propagation features of C# 6 with the .Net 4.0 because they are implemented purely by compiler
One way to tell the language version the compiler support is
csc -langversion:?
and get response like
Supported language versions:
default
1
2
3
4
5
6
7.0
7.1
7.2
7.3
8.0
9.0 (default)
latestmajor
preview
latest
To indicate to Visual Studio which language version to use there's a compiler option called /langversion
You can find more about it here.
It can be set programmatically too, as stated here.
The compiler can compile in different versions of the language, the language version is not directly related to that of the framework, but often to use a language feature there's a minimum framework for which it can work.
Just few minutes ago I have compiled in VS 2015 a dll which uses the string interpolation of c# 6.0
var version = 4;
var output = $"{version}";
and has the framework 4.0 as target.
It compiles and works fine.
This post explains versions entanglement, but only for older c# versions.