Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use C# 8 with Visual Studio 2017?

I'd like to use C# 8.0 (especially ranges and non-nullable reference types) in Visual Studio 2017. Is it possible?

like image 855
nonsensation Avatar asked Feb 15 '19 01:02

nonsensation


People also ask

How do I use C on my computer?

It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly. C is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute).

How is C used in everyday life?

C programming language is majorly used in the creation of hardware devices, operating systems, drivers, kernels, etc. It is also used for the development of GUIs and IDEs. For example: Linux Kernel is written in the C language.

How do I start learning C?

To get started with C or C++, you will want a compiler—although nowadays you can also learn C online by experimenting with “hello world” C projects in-browser. Compilers are programs that can be run through command-line interfaces (CLIs).


2 Answers

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):

enter image description here


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;         }     } } 
like image 133
Stephen Kennedy Avatar answered Sep 22 '22 02:09

Stephen Kennedy


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?

like image 44
Bear MAN Avatar answered Sep 22 '22 02:09

Bear MAN