文章预览
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 我们不能直接声明参
………………………………