Visual Studio 2022 Community: Stuck with C# language version 7.3? - c#

Trying to use sample code from the book "Learn C# programming..." by Bancila and Sharma.
When I try to build the project, I get error messages saying that certain features are not available in C# 7.3, and that I should upgrade to C# 9.0 resp. 10.0.
However, there is no way to change the language version. When I go to Project Properties/Build/Advanced, the language version shows frozen. Also, adding the line "<LangVersion>10.0</LangVersion>" in the .csprog file seems to have no effect.
Can somebody help or at least explain?
Thanks!
I added the line "<LangVersion>10.0</LangVersion>" in the .csprog file. But the problem remains.

The C# version depends on the .NET / .NET Framework version you are using.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

You'll need to download the relevant SDk to have access to newer versions of C#.
For C# 9, you need the .NET 5 SDK.
You can find it here:
https://dotnet.microsoft.com/en-us/download/dotnet

The book is targeted for C# 8 i.e. net core 3, so I would recommend just creating a project for .Net 6 or 7. These should be fully backward compatible with .net Core 3. In the visual studio installer you can select what specific .Net versions you want to include support for. I would expect .Net 6 to be installed by default, but it is easy to check.

Related

Visual Studio 2019, .NET 5 missing after installing SDK

I just installed the .NET 5 SDK, and created a new C# library.
The .NET 5 is missing from available frameworks. I've tried both standard, core and .net version of the library template.
As this was newly released, I can't find any hints on what I am missing with the good 'ole google search.
I was using VS2019 version 16.7.7, but 16.8 or later is required for .NET 5.0.
My bad. Should've checked the requirements before creating this question. Sorry

NRefactory capable of parsing .NET 4.6?

I have been successfully using NRefactory and just started having problems parsing source code that uses some newer .NET features. One feature in particular that I have noticed is string interpolation. When NRefactory sees something like $"File: {myFile}" it has total failure parsing it.
Is there a new version of the code that will handle dot-NET 4.6 or is there a flag that I need to set? If not, is there another easy way to get parse trees from source code?
If I wanted to use Roslyn, is there a way to do this with VS 2015 or is VS 2017 or later required? Tried opening Roslyn SLN in VS 2015 and none of the projects loaded.
NRefactory does not support C# 6 or newer features, which would impact string interpolation. According to NRefactory,
There is currently no maintainer for NRefactory. If you need a C#
parser / compiler frontend, use Microsoft.CodeAnalysis (Roslyn)
instead.
The refactorings in NRefactory have been ported to Roslyn:
https://github.com/icsharpcode/RefactoringEssentials/
You can use Roslyn with Visual Studio 2015, as long as you have .NET 4.6 or higher installed, though 4.7.2 is the recommended version to use by Roslyn team. Lastly, you will need to the following:
Install Universal Windows App Development Tools -> Tools (1.1.1) and
Windows SDK, Windows 8.1 and Windows Phone 8.0/8.1 tools -> Tools and
Windows SDKs and Visual Studio Extensibility Tools

Build server of an old ASP.NET 4 application fails building new C# 7, but it works in development

I recently start to work on a legacy ASP.NET codebase based on .NET framework 4.0. We managed to pass everything from Visual Studio 2012 to VS 2017, updated the build server with a new version of Jenkins and installing .NET framework 4.7.x.
Locally we can write C# code of the newest version (7.3) and the build works (VS doesn't use MSBuild if I remember right), but when we deploy on the build server the build fails because there MSBuild cannot recognize constructs newer than C# 4.0. To avoid mistakes I fixed the lang version to 4.0 (advanced build properties on projects), so if I write too new C# VS blocks me in dev, but we would like to start using new C#.
We also tried to fix C# 7.3 directly in the project (<LangVersion>7.3</LangVersion> in PropertyGroup inside csproj) and the but ToolsVersion property of Project element (csproj) to 14.0, but then building we MSBuild fails with the error:
CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3,
4, 5 or Default
Here it's explained that what I want to do it is possible: https://www.dotnetcurry.com/dotnet/1427/build-apps-different-dotnet-framework-versions
No matter which .NET framework version we target in the project, the
C# language version in use will not change. That’s fine because the
vast majority of language features that were introduced in later
versions of the language don’t depend on the CLR or specific APIs.
They are only syntactic sugar and the bytecode generated by the
compiler will still work in .NET framework 2.0.
Anyone have an idea of what mistake are we doing?
The problem was that on the build server MSBuild wasn't properly installed and build scripts got an old one.
Installing Visual Studio 2017 Build tools and fixing the path on the script we solved.
After we had the problem "The “GetReferenceNearestTargetFrameworkTask” task was not found" we solved like explained here: The "GetReferenceNearestTargetFrameworkTask" task was not found
(the right answer depends on what strategy have you used to install VS Buld tools).

In Which .Net Framework Version C# 7.2 is Available

I tried installing .net framework 4.7.2, still the target framework is empty in the project properties. The program I'm trying to run uses readonly struct, which is part C# 7.2.
Could you please guide me .Net Framework version has C# 7.2?
Language features are independent of .Net version (Framework/Core/Standard).
You will need to use a compiler that understands the newer language features. Assuming you're using Visual Studio 2017, you should update to latest version.
By default C# projects in Visual Studio use the latest major release of the language (which would be 7.0 as of this writing). To change this, open up the project's properties, select the Build page, click Advanced..., and under General set Langauge Version to "C# 7.2".
Per phuzi's comment mentioning this question, you might need to update to the latest Visual Studio in order to see this option.

Running C# v.<latest> on a Win 7 w/ .NET 4.0

We are writing an application that has to run on Windows 7... and we can't install a new version of the .NET framework on those client machines. As the developer, I want to use all the fancy new C# 6.0 language features, and if I understand correctly, the language and the framework have been decoupled.
I just need clarification: If I target C# 6.0 in my application, will the code still run correctly on a Win7 client with .NET 4.0 as the highest framework version?
Yes, you can use a C# 6 compiler while targeting an older version of .NET. The way this usually works is that you have a newer version of Visual Studio and target it at a specific .NET version. For C# 6, this means VS2015. You will be able to use any new C# features, as long as they don't rely on .NET libraries. In particular cases, such as if you want to use async stuff, there are backward compatibility libraries available.
You could also use csc.exe (C# compiler) directly, and bypass Visual Studio.

Categories