Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 455
Stacker Avatar asked Oct 04 '18 21:10

Stacker


People also ask

Why C is not working in VS code?

You have to change it so it runs in the terminal. Go to the menu Code > Preferences > Settings. In the User tab on the left panel, expand the Extensions section. Find and select Run Code Configuration.

How do I configure C and C++ in Visual Studio Code?

Install Visual Studio Code. Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X). Get the latest version of Mingw-w64 via MSYS2, which provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries.


2 Answers

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
like image 190
Jonathon Chase Avatar answered Sep 23 '22 12:09

Jonathon Chase


You just need to update your

<PropertyGroup> <LangVersion>4</LangVersion> </PropertyGroup> 

To the version you want

like image 30
Hawkzey Avatar answered Sep 24 '22 12:09

Hawkzey