Reputation: 3854
I have this situation:
public class FOO<T> where T : IBar
{
private T _xxx;
public Y(T xxx)
{
if (xxx == null) throw new ArgumentNullException("xxx");
_xxx = xxx;
}
}
public interface IBar
{
string XString { get; }
}
In the constructor, I am checking T
for null
. The compiler correctly warns me that I am checking for null
on something that may be a value type, as IBar
could be implemented by a struct.
How can I constraint T
to be a reference type?
Upvotes: 2
Views: 73
Reputation: 75326
The typical myth (even I got before) is types which derive from interface are implicitly reference types, but it's actually not. Struct also could have interface.
So, you should add more constraints class
to indicate as reference type
public class FOO<T> where T : class, IBar
Upvotes: 5