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: http://stackoverflow.com/questions/390900/cant-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?