views:

60

answers:

1

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?

+3  A: 

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

    return !(ReferenceEquals((object)result, (object)default(TDataSource)) 
          || result.Equals(default(TDataSource)));

If result is null (and a reference type), the ReferenceEquals call will return true, so Equals won't be called and won't throw a NullReferenceException.
If TDataSource is a value type, the ReferenceEquals will compare two different boxed references (which may happen to contain the same value, but will still be different), so it will pass on to the Equals call.

SLaks
Note that in general, this is how you have to compare two values of unknown type for equality. (i.e. this doesn't apply only to looking for default values)
Gabe
You're absolutely right. Are this only user defined structs that don't override the '==' operator?
JJoos
@JJoos, if value types override the `==` operator, it's considered best practice to override `Equals()` so that it behaves equivalently. This is not strictly guaranteed, but I would consider it a bug if I found a value type that did not behave in this way.
Dan Bryant
No need for something so complicated, you just need to use the static Object.Equals method : `Equals(result, default(TDataSource))`.
Thomas Levesque
@Thomas Levesque, Yeah also thought of that after @SLaks post. Seems to work correctly and is a lot more readable.
JJoos