什么是C#泛型方法中的“where T:class”?

什么是C#泛型方法中的“where T:class”?,第1张

概述这些方法签名有什么区别? public void T MyMethod<T>(T parameter) 和 public void T MyMethod<T>(T parameter) where T : class 它们似乎有相同的结果……那么T:class的作用是什么? 在第二种方法中,T只能是一个类,不能是结构类型. 见Constraints on Type Parameters (C# P 这些方法签名有什么区别?
public voID T MyMethod<T>(T parameter)

public voID T MyMethod<T>(T parameter) where T : class

它们似乎有相同的结果……那么T:class的作用是什么?

解决方法 在第二种方法中,T只能是一个类,不能是结构类型.

见Constraints on Type Parameters (C# Programming Guide):

where T : class

The type argument must be a reference [class] type; this applIEs also to any class,interface,delegate,or array type.

总结

以上是内存溢出为你收集整理的什么是C#泛型方法中的“where T:class”?全部内容,希望文章能够帮你解决什么是C#泛型方法中的“where T:class”?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1240165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存