加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#语法:其中TResponse,new()

发布时间:2020-12-15 19:45:24 所属栏目:百科 来源:网络整理
导读:public TResponse ExecuteCustomMessageTResponse(IModbusMessage request) where TResponse : IModbusMessage,new() 以上是什么意思?我以前从来没有见过这样的东西,虽然我已经在C#编写了几年了…它应该是一个函数,但我不确定这是什么 和关键字where和new()
public TResponse ExecuteCustomMessage<TResponse>(IModbusMessage request)
    where TResponse : IModbusMessage,new()

以上是什么意思?我以前从来没有见过这样的东西,虽然我已经在C#编写了几年了…它应该是一个函数,但我不确定这是什么< >和关键字where和new()在最后......

解决方法

它是TResponse泛型类型的 constraint,它实现了接口IModbusMessage并具有无参数构造函数.

where T : (interface name) The type argument must be or implement the
specified interface. Multiple interface constraints can be specified.
The constraining interface can also be generic.

where T : new() The type argument must have a public parameterless
constructor. When used in conjunction with other constraints,the
new() constraint must be specified last.

另外,正如其他人所指出的那样,我建议你阅读泛型文档(评论中由SLaks指出).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读