如何在.NET Framework及.NET6以前项目中使用C#12新特性

前两天发了一篇关于模式匹配的文章,链接地址,有小伙伴提到使用.NET6没法体验 C#新特性的疑问, 其实呢只要本地的SDK源代码编译器能支持到的情况下(直接下载VS2022或者VS的最新preview版本)
只需要做很小的改动就可以支持的.

目前仍然还有一些小伙伴因为历史原因可能还在写.NET Framework项目,

.NET Framework 目前不直接支持 C# 12,因为 C# 12 是与 .NET 6 和更高版本一起发布的,而 .NET Framework 的最后一个版本是 4.8,之后就不再更新了。但是,你也可以通过一些方法间接使用 C# 12 的新特性,前提是这些特性在编译时不依赖于 .NET 6 或更高版本的基础类库的新特性。以下是实现这一目标的步骤:

  1. 更新 Visual Studio:确保你的 Visual Studio 至少更新到支持 C# 12 的版本。这通常意味着使用 Visual Studio 2022 的最新版本。

  2. 编辑项目文件:手动编辑你的 .csproj 文件,将 C# 语言版本设置为 12。这可以通过添加或修改 <LangVersion> 标签来实现。

<PropertyGroup>
  <LangVersion>12.0</LangVersion>
</PropertyGroup>

当然如果安装的VS2022的最新preview版本你还可以把版本号直接设置为latest以支持比12.0更高的语言版本

当然如果你的代码可能需要同时支持多个框架版本你可以使用预编译指令,以避免编译错误的问题

#if NET8_0_OR_GREATER
// 使用 C# 12 的特性,这些特性依赖于 .NET8 或更高版本的基础类库
#else
// 提供 .NET Framework 兼容的代码
#endif

当设置LangVersion后,对于NET Framework项目如果使用record,init等关键字定义时应该还会有如下的报错出现,这主要是因为兼容.NET Standard版本实现的问题:
image

解决这个问题也很简单,项目中定义一个命名空间为System.Runtime.CompilerServicesIsExternalInitclass即可:

namespace System.Runtime.CompilerServices
{
    public static class IsExternalInit { }
}

下面创建一个项目测试一下,代码如下:

namespace UseLatestLangVersion
{
    public class TestClass(string initWord)
    {
        public int Id { get; init; }
        public record User(string Name, int Age);
        public void SayHello()
        {
            Console.WriteLine(initWord);
            var user = new User("vipwan", 30);
            if (user is { Age: >= 18 })
            {
                Console.WriteLine($"user {user.Name} is adult.");
            }
        }
    }
}

以上的代码项目框架是.NET Framework4.5.2,用到了最新的一些C#特性,经过简单改造,就可以编译成功了.

重中之重就是本地SDK的csc编译器支持的最大版本一定要>=LangVersion设置版本

最后请注意,即使通过这些方法,也不意味着早期的框架版本项目就能使用Nuget引用新版框架的dll库,比如NETFramework4.5引用NET8编译的dll版本,那肯定是不行的,除非该dll编译成了对应的框架版本支持

原创作者: vipwan 转载于: https://www.cnblogs.com/vipwan/p/18313947
This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including:, A Refined ADO.NET Entity Framework Programming ModelNumerous IDE and MVVM Enhancements for WPF Desktop DevelopmentNumerous updates to the ASP.NET Web APIs, This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies)., The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms., What You Will Learn:, Be the first to understand the .NET 4.6 platform and C# 6.Discover the ins and outs of the leading .NET technology.Learn from an award-winning author who has been teaching the .NET world since version 1.0.Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值