T是type1或type2的通用方法

编程入门 行业动态 更新时间:2024-10-27 06:25:26
本文介绍了T是type1或type2的通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有一种方法可以声明泛型类型为type1 或 type2的泛型函数?

Is there a way to declare a generic function that the generic type is of type1 or type2?

示例:

public void Foo<T>(T number) { }

我可以约束T为整型还是整型

Can I constraint T to be int or long

推荐答案

尽管您可以使用通用约束来限制类型不幸的是,对于每个泛型参数T,都没有一个可以让您在编译时强制执行T是 type1还是type2 。

Although you could use a generic constraint to limit the type of each generic argument T, unfortunately there is none that would allow you to enforce at compile time whether T is type1 or type2.

在编译时也没有任何方法可以强制您的泛型参数只能是任何原始类型(int,long,double,...)。

Nor is there any way to enforce at compile time that your generic argument can only be of any primitive type (int, long, double, ...).

更多推荐

T是type1或type2的通用方法

本文发布于:2023-11-05 15:58:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!