How do I create a C# 8.0 Console application? - c#

I tried creating a Windows Forms .NET core 3.1 application via the template and switching the output type to Console Application.
Here's my code:
static class Program
{
static void Main()
{
System.Console.WriteLine(0 switch { 0 => "Hello World" });
}
}
When I compile I get:
error CS8370: Feature 'recursive patterns' is not available in C# 7.3. Please use language version 8.0 or greater.
I'm targeting .NET Core 3.1. I thought that would get me C# 8.0 language features by default. Apparently I am mistaken.
What do I do?
EDIT: I'm using Visual Studio 2019 16.3.9
This is the part that confuses me the most because it says that the Language version is "Automatically selected based on framework version" (and I can't change it.) Also I don't see an adequate explanation of why I can't change language versions at Why can't I select a different C# version? That page says that if I'm using .NET Core 3.x that I should be using C# 8.0.
The .csproj file is as follows:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon />
<StartupObject>Program</StartupObject>
</PropertyGroup>
</Project>
Adding this line fixes the problem:
<LangVersion>8.0</LangVersion>
But is that really the only way to create an application? I have to manually edit my .csproj? Why can I not change the Language version and why is it not automatically selecting C# 8.0 based on me using .NET Core 3.1?

Open your csproj and see if you have a line like
<LangVersion>7.3</LangVersion>
If yes try removing it, if that doesn't work try to change it to 8.0
From https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults
You should remove the
<LangVersion>latest</LangVersion> from your project file when you
update the .NET SDK.

I had the same problem (c#8 unavailable on Core 3.1 project) and I fixed it that way :
Uninstall previous version of Visual studio (2015 and 2017 in my
case)
Repair Visual studio 2019 (V16.4.1 in my case) with "visual studio installer" launched with administrator right.
No need of LangVersion in csproj.

Related

Can I work with latest version of c# with visual studio 2013? [duplicate]

I'd like to use C# 8.0 (especially ranges and non-nullable reference types) in Visual Studio 2017. Is it possible?
Going forward, Microsoft want to tie C# language versions more closely to framework versions than they have in the past. They really only want you to be using C# 8 with .NET Core 3.x and .NET Standard 2.1 projects, and that means using Visual Studio 2019. My answer to Does C# 8 support the .NET Framework? has all the gory details.
However, if you really want to you can now use C# 8 in Visual Studio 2017 by using the same trick that brings C# 7 to Visual Studio 2015: install the latest version of the Microsoft.Net.Compilers Nuget package into the project. It works, but of course VS 2017 doesn't know about C# 8 syntax so it doesn't look very pretty. Here's a screenshot showing that VS 2017 is able to compile a small test library using nullable reference types and a static local method (both of which are C# 8 features):
Here's the .csproj and code if you want to try it:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers" Version="3.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
-
using System;
namespace CSharp8Test
{
public class Class1
{
public string? NullableString { get; } = "Test";
public static void Test()
{
Console.WriteLine(Test2());
static int Test2() => 5;
}
}
}
Add on to series0ne's comment about the major releases: It is true that new VS usually ship with new C# version. However, in the past experience, it is possible to upgrade previous version of VS to compiler newer version of C# code, mainly by upgrading the "Microsoft.Net.Compilers" Nuget package. You may reference to this post for more information. How to use c#7 with Visual Studio 2015?
You can Use Microsoft.Net.Compilers.Toolset instead of Microsoft.Net.Compilers in vs2017
This package is intended as a replacement for Microsoft.Net.Compilers (which is a Windows-only package) and Microsoft.NETCore.Compilers. Those packages are now deprecated and will be deleted in the future.
The package requires MSBuild 15.0 and either .NET Destkop 4.7.2 or .NET Core 2.1
The package versions:
The package version map the capability of c# 8 support compared to vs2019.
Version 3.0 includes a preview of C# 8.0 (Visual Studio 2019 version 16.0), but 2.11 was used for preview1.
Version 3.1 includes a preview of C# 8.0 (Visual Studio 2019 version 16.1)
Version 3.2 includes a preview of C# 8.0 (Visual Studio 2019 version 16.2)
Version 3.3 includes C# 8.0 (Visual Studio 2019 version 16.3, .NET Core 3.0)
How to use
Add these lines to the .csproj
<PropertyGroup>
//....
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable> <!-- to support nullable reference type -->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.3.1" PrivateAssets="All" />
</ItemGroup>
So the current info is:
Visual Studio 2019 has been released; it's no longer in preview.
C# 8 is still in preview (source).
The C# 8 beta compiler is available with Visual Studio 2019, or the latest .NET Core 3.0 preview. (source)
From this, I deduce that C# 8 is currently not available in VS2017, and that there are no plans to change that.
You can also use C# 8 in any .NET Project in Visual Studio 2019. https://dirkstrauss.com/enabling-c-8-in-visual-studio-2019/
There are some limitations though, but it is possible.

"Unable to find package Microsoft.NETCore.App.Host.osx-x64" after upgrading to .NET Core 3.0

Today, .NET Core 3.0 was released. It became available as an update in my Visual Studio for Mac, and decided to upgrade an ASP.NET Core project from .NET Core 2.2.3 to .NET Core 3.0. I thought it would be as simple as changing the Target framework in the Project Options:
and updating NuGet packages, but when trying to build the project I got the following error:
Unable to find package Microsoft.NETCore.App.Host.osx-x64 with version (= 2.2.3)
Cleaning the solution and restarting Visual Studio didn't help; is there somewhere else I have to change the .NET Core version?
It's not part of the default setup, but during the lifetime of the project I had to add a custom RuntimeFrameworkVersion setting in the .csproj file in order to make a specific package/component work. That setting was still pointing to version 2.2.3. AFAIK, there is no option to change this setting in Visual Studio itself, but if you open the project file you should be able to find it quite easily, right at the top of the file:
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<RuntimeFrameworkVersion>2.2.3</RuntimeFrameworkVersion>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
<Version>1.1</Version>
</PropertyGroup>
Removing the fifth line altogether (as recommended by #MartinUllrich in the comments) or changing it to
<RuntimeFrameworkVersion>3.0.0</RuntimeFrameworkVersion>
solved the problem for me.

visual studio 2019 version c# probleme

I can not change the version of C # on visual studio 2019
I tried to go to the property to access a window that normally allowed to change the version of C #
Error CS8025 The 'local functions' feature is not available in C # 4. Use version 7.0 or later.
I'm assuming you're using .Net Core. Within the .csproj file for your start-up project (executable, web, web API, whichever it is in your case), add the following:
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
See the docs for more details.
Right click on your csproj in the solution explorer.
Select properties.
Select 'Build' near the top left of the screen.
Click 'Advanced' in the main window (scroll down if you can't see it).
Change language version to latest minor version.
I was not able to edit the language in the property page.
I had to add this to the csproj which was not able to build.
in the propertyGroup, right where AsseblyName is for my .netcore 3 project.
<LangVersion>8</LangVersion>
you should probably use the version according to your .net framework version, the table is here:
The compiler determines a default based on these rules:
DEFAULTS
Target framework C# language version default
.NET 6.x C# 10
.NET 5.x C# 9.0
.NET Core 3.x C# 8.0
.NET Core 2.x C# 7.3
.NET Standard 2.1 C# 8.0
.NET Standard 2.0 C# 7.3
.NET Standard 1.x C# 7.3
.NET Framework all C# 7.3
source:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

How do I make VSCode use C# 7 instead of C# 4?

My VSCode debugger says that I'm using c# 4. I can't find any docs about getting VSCode to switch to C# 7. I'm on Windows 10. I don't know if this is a DotNet Core issue, a VSCode issue, or an Omnisharp issue. I would appreciate someone pointing me to a set of instructions or posts that solved this problem.
A snipet from the top of my VSCode .csproj file:
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0"
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <LangVersion>4</LangVersion> </PropertyGroup>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
These are my VSCode versions:
Version: 1.27.2 (user setup)
Commit: f46c4c469d6e6d8c46f268d1553c5dc4b475840f
Date: 2018-09-12T16:17:45.060Z
Electron: 2.0.7
Chrome: 61.0.3163.100
Node.js: 8.9.3 V8: 6.1.534.41
Architecture: x64
.NET Core SDK version 2.1.402 (x64)
As you've pointed out in the portion of your question where you posted the csproj, the current language version is set to 4.
<LangVersion>4</LangVersion>
You can get the latest features for C# 7.3 by setting it to <LangVersion>7.3</LangVersion>
If you merely want the C# 7.0 features, you can set the value to <LangVersion>7</LangVersion>
This document lists the (currently) valid options that can be passed to the -langversion compiler flag. If you want your project to always compile with the latest available version of C#, you can set the value to <LangVersion>latest</LangVersion> in your .csproj. Of course, this will only compile to the latest version supported by your version of the compiler.
The behavior of the default value has changed in recent versions of the compiler. The document accurate at the time of editing is this one, which states the following:
The compiler determines a default based on these rules:
Target framework
Version
C# language version default
.NET Core
6.x
C# 10.0
.NET Core
5.x
C# 9.0
.NET Core
3.x
C# 8.0
.NET Core
2.x
C# 7.3
.NET Standard
2.1
C# 8
.NET Standard
1.x/2.0
C# 7.3
.NET Framework
all
C# 7.3
You just need to update your
<PropertyGroup> <LangVersion>4</LangVersion> </PropertyGroup>
To the version you want

Visual Studio 2010 isn't loading my solution [duplicate]

Today I installed the .NET Framework 4.5 on my machine expecting to be able to use it from Visual Studio 2010, since it's just a minor update that should't pose problems for Visual Studio 2010. Unfortunately I am not, even manually removing certain 4.0 and adding the corresponding 4.5 assemblies resulted in the original 4.0 assemblies still being referenced in the project.
Is it possible to target version 4.5 from Visual Studio 2010 and if yes, how? I'd really like to use the ribbons...
Each version of Visual Studio prior to Visual Studio 2010 is tied to a specific .NET framework. (VS2008 is .NET 3.5, VS2005 is .NET 2.0, VS2003 is .NET1.1) Visual Studio 2010 and beyond allow for targeting of prior framework versions but cannot be used for future releases. You must use Visual Studio 2012 in order to utilize .NET 4.5.
There are pretty limited scenarios that I can think of where this would be useful, but let's assume you can't get funds to purchase VS2012 or something to that effect. If that's the case and you have Windows 7+ and VS 2010 you may be able to use the following hack I put together which seems to work (but I haven't fully deployed an application using this method yet).
Backup your project file!!!
Download and install the Windows 8 SDK which includes the .NET 4.5 SDK.
Open your project in VS2010.
Create a text file in your project named Compile_4_5_CSharp.targets with the following contents. (Or just download it here - Make sure to remove the ".txt" extension from the file name):
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Change the target framework to 4.5 if using the ".NET 4.5" configuration -->
<PropertyGroup Condition=" '$(Platform)' == '.NET 4.5' ">
<DefineConstants Condition="'$(DefineConstants)'==''">
TARGETTING_FX_4_5
</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)'!='' and '$(DefineConstants)'!='TARGETTING_FX_4_5'">
$(DefineConstants);TARGETTING_FX_4_5
</DefineConstants>
<PlatformTarget Condition="'$(PlatformTarget)'!=''"/>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<!-- Import the standard C# targets -->
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- Add .NET 4.5 as an available platform -->
<PropertyGroup>
<AvailablePlatforms>$(AvailablePlatforms),.NET 4.5</AvailablePlatforms>
</PropertyGroup>
</Project>
Unload your project (right click -> unload).
Edit the project file (right click -> Edit *.csproj).
Make the following changes in the project file:
a. Replace the default Microsoft.CSharp.targets with the target file created in step 4
<!-- Old Import Entry -->
<!-- <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> -->
<!-- New Import Entry -->
<Import Project="Compile_4_5_CSharp.targets" />
b. Change the default platform to .NET 4.5
<!-- Old default platform entry -->
<!-- <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> -->
<!-- New default platform entry -->
<Platform Condition=" '$(Platform)' == '' ">.NET 4.5</Platform>
c. Add AnyCPU platform to allow targeting other frameworks as specified in the project properties. This should be added just before the first <ItemGroup> tag in the file
<PropertyGroup Condition="'$(Platform)' == 'AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
.
.
.
<ItemGroup>
.
.
.
Save your changes and close the *.csproj file.
Reload your project (right click -> Reload Project).
In the configuration manager (Build -> Configuration Manager) make sure the ".NET 4.5" platform is selected for your project.
Still in the configuration manager, create a new solution platform for ".NET 4.5" (you can base it off "Any CPU") and make sure ".NET 4.5" is selected for the solution.
Build your project and check for errors.
Assuming the build completed you can verify that you are indeed targeting 4.5 by adding a reference to a 4.5 specific class to your source code:
using System;
using System.Text;
namespace testing
{
using net45check = System.Reflection.ReflectionContext;
}
When you compile using the ".NET 4.5" platform the build should succeed. When you compile under the "Any CPU" platform you should get a compiler error:
Error 6: The type or namespace name 'ReflectionContext' does not exist in
the namespace 'System.Reflection' (are you missing an assembly reference?)
FYI, if you want to create an Installer package in VS2010, unfortunately it only targets .NET 4. To work around this, you have to add NET 4.5 as a launch condition.
Add the following in to the Launch Conditions of the installer (Right click, View, Launch Conditions).
In "Search Target Machine", right click and select "Add Registry Search".
Property: REGISTRYVALUE1
RegKey: Software\Microsoft\NET Framework Setup\NDP\v4\Full
Root: vsdrrHKLM
Value: Release
Add new "Launch Condition":
Condition: REGISTRYVALUE1>="#378389"
InstallUrl: http://www.microsoft.com/en-gb/download/details.aspx?id=30653
Message: Setup requires .NET Framework 4.5 to be installed.
Where:
378389 = .NET Framework 4.5
378675 = .NET Framework 4.5.1 installed with Windows 8.1
378758 = .NET Framework 4.5.1 installed on Windows 8, Windows 7 SP1, or Windows Vista SP2
379893 = .NET Framework 4.5.2
Launch condition reference: http://msdn.microsoft.com/en-us/library/vstudio/xxyh2e6a(v=vs.100).aspx
I have been struggling with VS2010/DNFW 4.5 integration and have finally got this working. Starting in VS 2008, a cache of assemblies was introduced that is used by Visual Studio called the "Referenced Assemblies". This file cache for VS 2010 is located at \Reference Assemblies\Microsoft\Framework.NetFramework\v4.0. Visual Studio loads framework assemblies from this location instead of from the framework installation directory. When Microsoft says that VS 2010 does not support DNFW 4.5 what they mean is that this directory does not get updated when DNFW 4.5 is installed. Once you have replace the files in this location with the updated DNFW 4.5 files, you will find that VS 2010 will happily function with DNFW 4.5.
From another search. Worked for me!
"You can use Visual Studio 2010 and it does support it, provided your OS supports .NET 4.5.
Right click on your solution to add a reference (as you do). When the dialog box shows, select browse, then navigate to the following folder:
C:\Program Files(x86)\Reference Assemblies\Microsoft\Framework\.Net Framework\4.5
You will find it there."

Categories

Resources