Reputation: 8317
I think it is a bit strange, I want "I" to be only a interface type, and T being a class implenenting that interface type (or extending it).
Seems either I'm unable to set "null" value or to force correctly to just restrict the template to interface type.
public class MyClass< T, I> where T: I
{
private I myInterface = null; // error here. I have to use "default(I)"
}
Am I forcing correctly to have I "interface" ? otherwise why should I have to use "default(I)"?
The reason to use I is because T has overloaded null check operator (I'm using Unity3D and hence I have to check equality over interface to avoid a bottleneck.
Well in reality I have:
public class MyClass< T, I>: MonoBehaviour, where T: MonoBehaviour, I
{
private I myInterface = null; // error here. I have to use "default(I)"
}
Upvotes: 1
Views: 34
Reputation: 156948
Because the code below is a valid implementation to your generic type parameters, you can't be 100% sure T
is a class (the only type being nullable):
MyClass<int, IEquatable<int>> m;
However, using the class
constraint, it is:
public class MyClass<T, I> where T: I where I : class
{
private I myInterface = null; // <-- now it is valid
}
Upvotes: 2