我有这个:
public IClub GetTeam()
{
return new Arsenal();
}
//compiles since Arsenal is an IClub
public T GetTeam<T>() where T : IClub, new()
{
return new Arsenal();
}
//wouldn't compile saying "cannot convert Arsenal to T"
但这些东西有效:
public T GetTeam<T>() where T : IClub, new()
{
T t = new T();
t.Sponsor = "Nike"; //since it knows T is IClub,
return new T(); //but why the injustice to return type alone?
}
为什么即使返回类型仍然存在,第二个代码块也不会编译
IClub
?这不是不公平吗?我知道我没有充分利用上述代码中类型约束的潜力,但是让代码运行的替代方法是什么?