I just ran into this situation and i thought it was a nice opportunity to use the default keyword. But it doesn’t compile and i can’t think of why. The example below illustrates my problem:
public class Test<TDataSource>
{
public IQueryable<TDataSource> DataSource { get; set; }
public bool GetOneOrDefaultResult()
{
var result = DataSource.SingleOrDefault();
return result != default(TDataSource);
}
}
You’ll get an error on line 8 (“Operator ‘==’ cannot be applied to operands of type ‘TDataSource’ and ‘TDataSource’.”). I thought using the default keyword would eliminate any comparison problems between reference types and value types.
Adding a generic constraint limiting TDataSource to reference types makes this piece of code compile.
Can somebody explain why the compiler won’t fix this for me? Is it just not smart enough to see this would work?
This is related:
Can't operator == be applied to generic types in C#?
[Edit]
SLaks answer gave me some inspiration, the ‘==’ operator won’t work, but the Equals function should.
public class Test<TDataSource>
{
public IQueryable<TDataSource> DataSource { get; set; }
public bool GetOneOrDefaultResult()
{
var result = DataSource.SingleOrDefault();
return result.Equals(default(TDataSource));
}
}
This compiles would this function correctly?
You cannot assume that every value type overrides the
==operator. (And even if they did, there would be no way to call it using generics; it’s a static method)Instead, you should write
If
resultisnull(and a reference type), theReferenceEqualscall will returntrue, soEqualswon’t be called and won’t throw aNullReferenceException.If
TDataSourceis a value type, theReferenceEqualswill compare two different boxed references (which may happen to contain the same value, but will still be different), so it will pass on to theEqualscall.