当前位置:网站首页>C 11 new feature: static abstract members in interfaces
C 11 new feature: static abstract members in interfaces
2022-06-13 09:44:00 【Dotnet cross platform】
Before
Suppose we have a very complicated mathematical operation :
public static int Calc(int x, int y) => x + y; however , The above method can only support int type . If you need to pass in other numeric types , Need to define again :
public static double Calc(double x, double y) => x + y;For this need , We prefer to adopt a paradigm approach to realize :
public static T Calc<T>(T x, T y) => x + y;however , Operator “+” Cannot be applied to “T” and “T” Operands of type :

C# 11
quote System.Runtime.Experimental NuGet package , And add... To the project file <EnablePreviewFeatures>True</EnablePreviewFeatures>.
Define the following method :
public static T Calc<T>(T x, T y) where T : INumber<T> => x + y;Now? , No matter what data type is passed , Can be executed normally :
Calc(4, 5);
Calc(4.0, 5.0); This is because , We use System.Runtime.Experimental Define the number type ,int、double All inherited INumber Interface :

and INumber The interface inherits IAdditionOperators Interface , The operator is defined “+”:
public interface IAdditionOperators<TSelf, TOther, TResult> where TSelf : IAdditionOperators<TSelf, TOther, TResult>
{
public static abstract TResult operator +(TSelf left, TOther right);
}Here we use C# 11 New features available , You can add static abstract members to the interface .
The main scenario for this functionality is the use of mathematical operators in generic types , Because operators must be defined as statically abstract :

besides , You can also define other static members and static attributes in the interface :
public interface IDemo
{
public static abstract string StaticProperty { get; set; }
}Add microsignals 【MyIO666】, Invite you to join the technical exchange group
边栏推荐
- Sequential representation of linear tables
- Tree and binary tree: storage structure of binary tree
- C# 11 新特性:接口中的静态抽象成员
- Node-RED系列(二四):在Node-RED中使用mysql节点实现数据库的增删改查
- I have summarized the knowledge points of JS [intermediate and advanced] for you
- matlab轮毂电机分析模糊pid控制垂向振动分析
- 信息文档管理与配置管理
- It was so simple to implement system call
- MySQL利用E-R模型的数据库概念设计
- LeetCode 343. integer partition
猜你喜欢
随机推荐
LeetCode 322. Change
(dfs) acwing 842. Arrange numbers
ThingsBoard教程(二十):使用规则链过滤遥测数据
【pytorch环境安装搭建】
(state compression dp+good) acwing 291 Mondrian's dream
Protocol UART of schematic diagram
【 ssl2 ⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶83
Node-RED系列(二五):集成高德地图并实现3d地图和轨迹回放
[Luogu p1090, ssl1040] merged fruit [pile]
Exercise 7-10 finding specified characters (15 points)
MySQL利用E-R模型的数据库概念设计
VDD, dvdd, avdd, VCC, afvdd, dovdd, iovdd
Mobile web effects
第一章 第一节
[51nod p3111] xiaoming'ai intercepts [Las]
(dijkstra+ shortest path + edge traversal 0 (m)) acwing 850 Dijkstra finding the shortest path II
Memory management -- Viewing memory space from the perspective of executing programs and processes
二叉树简介
Jenkins accédant à l'authentification de l'utilisateur openldap
[51nod p3395] n-bit gray code [bit operation]









