你能幫我理解在這種情況下的錯誤嗎?簡單的通用誤解
public interface IGeneralInterface
{
}
public class A : IGeneralInterface
{
}
public class B : IGeneralInterface
{
}
public class SomeClass<TGenericType> where TGenericType : IGeneralInterface
{
private TGenericType internalValue;
public SomeClass(TGenericType InitValue)
{
internalValue = InitValue;
}
public TGenericType CreateAnother()
{
TGenericType val1 = new B(); //Error here: class B() could not be converted to TGenericType
return val1;
}
}
即使我建立SomeClass<T>
作爲
SomeClass<IGeneralInterface> someClass = new SomeClass<IGeneralInterface>();
我明確地傳遞基本接口包括所有(?)的情況下,它仍然拋出一個錯誤
如果你有'SomeClass someA'?當'TGenericType'爲'A'時,如何返回'B'? – Kobi