c#泛型中的类型演绎类似于c++模板
Type deduction in C# generics like in C++ templates?
在c#中:是否可以使用泛型参数类的类型作为方法的返回类型或定义成员?
c++的例子:class AgrumentClass
{
typedef int type;
public:
static type GetSomething() { return 0; }
};
template< class T >
class GenericClass
{
typedef typename T::type type;
public:
static type GetSomething() { return T::GetSomething(); }
};
int main()
{
int value = GenericClass<AgrumentClass>::GetSomething();
return value;
}
这样的例子没有一个清晰的c#翻译,因为它对泛型类型上可用的方法做了一个假设。c#允许您将泛型类型限制为特定类型或接口的后代或实现者,但这是您所能做到的。如果该方法不能在编译时解析为该类型,则它将无法编译。
相关文章:
- 类似于strcat()的函数出现问题
- 如何将C++闭包与变量参数同时重用——类似于JavaScript
- 算术运算的结果类似于:C浮点变量中的1/3
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 创建类似于布尔值的变量类型
- 如何在 Arduino 字符串的开头添加元素.类似于 JS unshift();
- Java 中是否有类似于 C++ 中引用类型"&"的内容?
- C++未知长度字符串的数组,其行为类似于 Python 字符串列表
- 如何创建类似于 QVariant 的变体类
- 对于多个字符(如 ETX/STX 对),是否有类似于 std::quote 的东西
- 初始化命名空间中的变量是否类似于将它们初始化为类成员?
- C++中用于结构的纯数组的类似于TableView/DataFrame的通用功能
- 有没有类似于stoi的函数可以用于模板类
- 如何编写一个类似于kernellist_head的c++风格的双链表实现
- 序列化模式类似于boost::序列化
- 构造一个类似于JSON文件c++的字符串
- 多重继承中的派生类的行为类似于聚合
- 如何使更安全的C++变体访问者,类似于切换语句?
- C++ Python 中是否有类似于 help() 的函数
- 是否可以为成员的方法编写类似于固有方法的 using 声明?