“范型”总是让我联想到那几乎无所不能的C++ Template,可这C#的范型却让我抓狂。
C#中编译如下代码,
Cannot create an instance of the variable type 'T' because it does not have the new() constraint
抓狂,不是说范型嘛,我还没说T是什么类型呢,编辑器就说does not have the new() constraint, 公然“强奸”我的意志,Bull shit。谁来拯救我这无知的人啊!
C#中编译如下代码,
class A<T>
{
public A()
{
this.member1 = new T();
}
private T member1;
}
这代码看上去多自然啊,居然报错:{
public A()
{
this.member1 = new T();
}
private T member1;
}
Cannot create an instance of the variable type 'T' because it does not have the new() constraint
抓狂,不是说范型嘛,我还没说T是什么类型呢,编辑器就说does not have the new() constraint, 公然“强奸”我的意志,Bull shit。谁来拯救我这无知的人啊!