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

后端 未结 5 1977
你的背包
你的背包 2020-12-05 09:38

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

5条回答
  •  臣服心动
    2020-12-05 10:18

    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:

    
      
        netstandard2.0;net452
        8.0    
        enable
      
      
        
          all
          runtime; build; native; contentfiles; analyzers
        
      
    
    

    -

    using System;
    
    namespace CSharp8Test
    {
        public class Class1
        {
            public string? NullableString { get; } = "Test";
    
            public static void Test()
            {
                Console.WriteLine(Test2());
                static int Test2() => 5;
            }
        }
    }
    

提交回复
热议问题