How can I use C# 8 with Visual Studio 2017?(如何在 Visual Studio 2017 中使用 C# 8?)
问题描述
我想在 Visual Studio 2017 中使用 C# 8.0(尤其是范围和不可为空的引用类型).可以吗?
展望未来,微软希望将 C# 语言版本与过去的框架版本更紧密地联系在一起.他们真的只希望您将 C# 8 与 .NET Core 3.x 和 .NET Standard 2.1 项目一起使用,这意味着使用 Visual Studio 2019.我对
<小时>如果您想尝试一下,这里是 .csproj 和代码:
<Project Sdk="Microsoft.NET.Sdk"><属性组><TargetFrameworks>netstandard2.0;net452</TargetFrameworks><LangVersion>8.0</LangVersion><Nullable>启用</Nullable></属性组><项目组><PackageReference Include="Microsoft.Net.Compilers" 版本="3.3.1"><PrivateAssets>所有</PrivateAssets><IncludeAssets>运行时;建造;本国的;内容文件;分析器</IncludeAssets></包参考></项目组></项目>-
使用系统;命名空间 CSharp8Test{公共课 Class1{公共字符串?NullableString { 得到;} = "测试";公共静态无效测试(){Console.WriteLine(Test2());静态 int Test2() =>5个;}}}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;
}
}
}
这篇关于如何在 Visual Studio 2017 中使用 C# 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Visual Studio 2017 中使用 C# 8?
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
