13

In the code-sample below, the compiler complains on x.Id == reference.Id:

Operator '==' cannot be applied to operands of type 'TId' and 'TId'

Similar questions have been asked on SO and they're solved by replacing the ==-operator with IEquatable<> + Equals or EqualityComparer<TEnum>.Default.

However, both solutions don't work for me for reasons that are not important to this question.

I'm not looking for a replacement for the == operator, I'm looking for an explanation why the equality operator doesn't work for generic types.

public class Object<TId>
{
    public TId Id { get; set; }

    // Some other object properties...
}

public class ObjectReference<TId>
{
    public TId Id { get; set; }
}

public class ObjectStore<TId>
{
    private List<Object<TId>> _store = new List<Object<TId>>();

    public Object<TId> FindByReference(ObjectReference<TId> reference)
    {
        return _store.FirstOrDefault(x => x.Id == reference.Id);
    }
}
0

2 Answers 2

24

I'm not looking for a replacement for the == operator, I'm looking for an explanation why the compiler can't figure out that both generic properties are of the same type.

There is no explanation that explains a falsehood. The compiler can and does figure out that both generic properties are of the same compile-time type, which you could illustrate with something like:

x.Id = reference.Id;

The compiler would allow that assignment no problem because it knows that there is an identity conversion between two identical at compile time types.

So you must be looking for an explanation of some other thing. I think what you are really looking for is a justification for why operator overload resolution fails to find a best operator for equality on a type parameter.

The answer is: C# generic types are not C++ templates. In C++, if you have ex1 OP ex2 then the resolution of the operator that determines its semantics is performed once per construction of the template. In C#, we don't do that; we perform overload resolution on operators once and must find an operator that works for all possible substitutions of type arguments.

We cannot do that for equality operators on unconstrained types; if TId is object then reference equality must be performed; if it is string then string equality must be performed, if it is int then int equality must be performed, if it is "nullable Guid", then lifted-to-nullable Guid equality must be performed, and so on. There is no generalized equality operator in C#, only a collection of specific equality operators, and since there is no generalized operator, there's no single operator for operator overload resolution to choose. Thus you get an error.

That's why in order to do this you would typically constrain the type to implement some interface that can be used; we can generically call interface methods on generic types.

You've rejected that correct solution to the problem, and so there's not much we can do to help you here without knowing more about why you've rejected the standard, safe, efficient solution.

Now, you might note that the compiler could generate code which determines at runtime what the resolution of the overload resolution algorithm is, based on the runtime types. C# cannot do that without excessive performance cost; if you are willing to pay that cost, then cast your operands to dynamic. That tells the compiler that you are willing to accept overload resolution failures at runtime in exchange for not getting them at compile time; be careful! When you turn off a safety system, you are responsible for ensuring the type safety of your program.

1
  • 2
    You're right, I didn't realize that there's no single == operator that works for all types. This is the kind of answer I was looking for in order to fully understand this limitation. Still learning every day. Thanks for your time Eric! Oct 23, 2017 at 18:12
3

Since you are talking about problems with Entity Framework, I can assume that your _store is not really as List like in your sample code (with that you won't have problems using other mentioned methods) but some form of IQueryable. Then you should be able to do what you want by building Expression manually, like this:

public class ObjectStore<TId> {
    private readonly IQueryable<Object<TId>> _store;

    public ObjectStore(IQueryable<Object<TId>> store) {
        _store = store;
    }

    public Object<TId> FindByReference(ObjectReference<TId> reference) {
        var refId = reference.Id;
        // x =>
        var arg = Expression.Parameter(typeof(Object<TId>), "x");
        // x.Id == refId
        var equals = Expression.Equal(Expression.Property(arg, "Id"), Expression.Constant(refId));
        // x => x.Id == refId
        var where = (Expression<Func<Object<TId>, bool>>) Expression.Lambda(equals, arg);
        return _store.FirstOrDefault(where);
    }
}

You can apply the same method to your sample code too though, by compiling expression with Compile() and passing resulting Func to FirstOrDefault, though I won't recommend doing that in practice unless really necessary (say you want to really call == operator and nothing else).

1
  • Thanks Evk for your time and effort. I've changed my question because it generated some confusion about why I wanted to know why == operator behaved the way it was. While your answer would fix my particular issue, it didn't clear up the confusion I had. You're answer could be seen as a duplicate to this one Oct 24, 2017 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.