专栏名称: dotNET跨平台
专注于.NET Core的技术传播。在这里你可以谈微软.NET,Mono的跨平台开发技术。在这里可以让你的.NET项目有新的思路,不局限于微软的技术栈,横跨Windows,Linux 主流平台
今天看啥  ›  专栏  ›  dotNET跨平台

C# 13 Ref Struct Interfaces

dotNET跨平台  · 公众号  ·  · 2024-09-23 08:00
    

文章预览

C# 13 Ref Struct Interfaces Intro C# 从 7.2 开始引入了 ref struct , ref struct 只能分配在栈上,不能被装箱,因为不能被装箱之前的版本中 ref struct 是不能实现接口的,在转成接口的时候会导致发生装箱,这是不被允许的,而我们在做一些设计的时候往往会使用到接口,用接口定义契约 contract,C# 13 开始我们可以允许 ref struct 实现接口,并且增加了可以作为泛型类型约束允许 ref struct 类型 Sample 来看一个简单的示例: file  interface   IAge {      public   int   GetAge ( ) ; } file  ref   struct  Age : IAge {      public   int   GetAge ( )  =>  1 ; } 使用的地方需要这样用: private   static   void  PrintAge (TAge age)      where  TAge : IAge, allows  ref   struct {     Console.WriteLine(age.GetAge()); } 方法需要声明为泛型参数,并且要指定泛型约束 allows ref struct 我们不能直接声明参 ………………………………

原文地址:访问原文地址
快照地址: 访问文章快照
总结与预览地址:访问总结与预览