Can't operator == be applied to generic types in C#?
Asked Answered
H

14

407

According to the documentation of the == operator in MSDN,

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings. User-defined value types can overload the == operator (see operator). So can user-defined reference types, although by default == behaves as described above for both predefined and user-defined reference types.

So why does this code snippet fail to compile?

bool Compare<T>(T x, T y) { return x == y; }

I get the error Operator '==' cannot be applied to operands of type 'T' and 'T'. I wonder why, since as far as I understand the == operator is predefined for all types?

Edit: Thanks, everybody. I didn't notice at first that the statement was about reference types only. I also thought that bit-by-bit comparison is provided for all value types, which I now know is not correct.

But, in case I'm using a reference type, would the == operator use the predefined reference comparison, or would it use the overloaded version of the operator if a type defined one?

Edit 2: Through trial and error, we learned that the == operator will use the predefined reference comparison when using an unrestricted generic type. Actually, the compiler will use the best method it can find for the restricted type argument, but will look no further. For example, the code below will always print true, even when Test.test<B>(new B(), new B()) is called:

class A { public static bool operator==(A x, A y) { return true; } }
class B : A { public static bool operator==(B x, B y) { return false; } }
class Test { void test<T>(T a, T b) where T : A { Console.WriteLine(a == b); } }
Heartwood answered 24/12, 2008 at 6:32 Comment(3)
It might be useful to understand that even without generics, there are some types for which the == is not allowed between two operands of the same type. This is true for struct types (except "pre-defined" types) which do not overload the operator ==. As a simple example, try this: var map = typeof(string).GetInterfaceMap(typeof(ICloneable)); Console.WriteLine(map == map); /* compile-time error */Ole
Continuing my own old comment. For example (see other thread), with var kvp1 = new KeyValuePair<int, int>(); var kvp2 = kvp1;, then you cannot check kvp1 == kvp2 because KeyValuePair<,> is a struct, it is not a C# pre-defined type, and it does not overload the operator ==. Yet an example is given by var li = new List<int>(); var e1 = li.GetEnumerator(); var e2 = e1; with which you cannot do e1 == e2 (here we have the nested struct List<>.Enumerator (called "List`1+Enumerator[T]" by the runtime) which does not overload ==).Ole
Equally valuable almost-duplicate (so not voting to close): Null or default comparison of generic argument in C#Blancheblanchette
C
166

"...by default == behaves as described above for both predefined and user-defined reference types."

Type T is not necessarily a reference type, so the compiler can't make that assumption.

However, this will compile because it is more explicit:

    bool Compare<T>(T x, T y) where T : class
    {
        return x == y;
    }

Follow up to additional question, "But, in case I'm using a reference type, would the the == operator use the predefined reference comparison, or would it use the overloaded version of the operator if a type defined one?"

I would have thought that == on the Generics would use the overloaded version, but the following test demonstrates otherwise. Interesting... I'd love to know why! If someone knows please share.

namespace TestProject
{
 class Program
 {
    static void Main(string[] args)
    {
        Test a = new Test();
        Test b = new Test();

        Console.WriteLine("Inline:");
        bool x = a == b;
        Console.WriteLine("Generic:");
        Compare<Test>(a, b);

    }


    static bool Compare<T>(T x, T y) where T : class
    {
        return x == y;
    }
 }

 class Test
 {
    public static bool operator ==(Test a, Test b)
    {
        Console.WriteLine("Overloaded == called");
        return a.Equals(b);
    }

    public static bool operator !=(Test a, Test b)
    {
        Console.WriteLine("Overloaded != called");
        return a.Equals(b);
    }
  }
}

Output

Inline: Overloaded == called

Generic:

Press any key to continue . . .

Follow Up 2

I do want to point out that changing my compare method to

    static bool Compare<T>(T x, T y) where T : Test
    {
        return x == y;
    }

causes the overloaded == operator to be called. I guess without specifying the type (as a where), the compiler can't infer that it should use the overloaded operator... though I'd think that it would have enough information to make that decision even without specifying the type.

Chamber answered 24/12, 2008 at 6:44 Comment(5)
Re: Follow Up 2: Actually the compiler will link it the best method it finds, which is in this case Test.op_Equal. But if you had a class that derives from Test and overrides the operator, then Test's operator will still be called.Heartwood
I good practice that I would like to point out is that you should always do the actual comparison inside an overridden Equals method (not in the == operator).Tann
Overload resolution happens compile-time. So when we have == between generic types T and T, the best overload is found, given what constraints are carried by T (there's a special rule that it will never box a value-type for this (which would give a meaningless result), hence there must be some constraint guaranteeing it's a reference type). In your Follow Up 2, if you come in with DerivedTest objects, and DerivedTest derives from Test but introduces a new overload of ==, you will have the "problem" again. Which overload is called, is "burned" into the IL at compile-time.Ole
wierdly this seems to work for general reference types (where you would expect this comparison to be on reference equality) but for strings it seems to also use reference equality - so you can end up comparing 2 identical strings and having == (when in a generic method with the class constraint) say they are different.Storybook
It's because the signature doesn't match -- your constraint is for class, which effectively means that, at compile time, these are both instances of type object -- your == operator requires that both operands be of type Test, so, that operator won't be called due to a signature mismatch (remember, method and operator signature matching happens at compile time!) -- Same thing would happen if you boxed an instance of the Test class, btw. (i.e. Test a = new Test(); object b = new Test(); Console.WriteLine(a == b); will always return false.)Oleaginous
A
364

As others have said, it will only work when T is constrained to be a reference type. Without any constraints, you can compare with null, but only null - and that comparison will always be false for non-nullable value types.

Instead of calling Equals, it's better to use an IComparer<T> - and if you have no more information, EqualityComparer<T>.Default is a good choice:

public bool Compare<T>(T x, T y)
{
    return EqualityComparer<T>.Default.Equals(x, y);
}

Aside from anything else, this avoids boxing/casting.

Aymer answered 24/12, 2008 at 7:32 Comment(5)
Minor aside, Jon; you might want to note the comment re pobox vs yoda on my post.Orvilleorwell
Nice tip on using EqualityComparer<T>Steen
Great answer, it also answers a question I had (12 years after you posted your answer ;-)) - I needed a generic function to check if a variable contains a default value. It was easy to write one returning a default value: T GetDefault<T>() => (T)default; But I couldn't write a function IsDefault<T>(value) without restricting it to IComparable which didn't work for all types. With your answer it is now simple: bool IsDefault<T>(T value) => Compare((T)default, value); is the answer to my question. Thanks, Jon!Inch
Could you please update the answer with C#11 generic math option? (There is no way current answer with 2 votes will ever be found and adding that info by anyone else would be rejected)Sympathy
@AlexeiLevenkov: No. I've upvoted Marc's answer though.Aymer
C
166

"...by default == behaves as described above for both predefined and user-defined reference types."

Type T is not necessarily a reference type, so the compiler can't make that assumption.

However, this will compile because it is more explicit:

    bool Compare<T>(T x, T y) where T : class
    {
        return x == y;
    }

Follow up to additional question, "But, in case I'm using a reference type, would the the == operator use the predefined reference comparison, or would it use the overloaded version of the operator if a type defined one?"

I would have thought that == on the Generics would use the overloaded version, but the following test demonstrates otherwise. Interesting... I'd love to know why! If someone knows please share.

namespace TestProject
{
 class Program
 {
    static void Main(string[] args)
    {
        Test a = new Test();
        Test b = new Test();

        Console.WriteLine("Inline:");
        bool x = a == b;
        Console.WriteLine("Generic:");
        Compare<Test>(a, b);

    }


    static bool Compare<T>(T x, T y) where T : class
    {
        return x == y;
    }
 }

 class Test
 {
    public static bool operator ==(Test a, Test b)
    {
        Console.WriteLine("Overloaded == called");
        return a.Equals(b);
    }

    public static bool operator !=(Test a, Test b)
    {
        Console.WriteLine("Overloaded != called");
        return a.Equals(b);
    }
  }
}

Output

Inline: Overloaded == called

Generic:

Press any key to continue . . .

Follow Up 2

I do want to point out that changing my compare method to

    static bool Compare<T>(T x, T y) where T : Test
    {
        return x == y;
    }

causes the overloaded == operator to be called. I guess without specifying the type (as a where), the compiler can't infer that it should use the overloaded operator... though I'd think that it would have enough information to make that decision even without specifying the type.

Chamber answered 24/12, 2008 at 6:44 Comment(5)
Re: Follow Up 2: Actually the compiler will link it the best method it finds, which is in this case Test.op_Equal. But if you had a class that derives from Test and overrides the operator, then Test's operator will still be called.Heartwood
I good practice that I would like to point out is that you should always do the actual comparison inside an overridden Equals method (not in the == operator).Tann
Overload resolution happens compile-time. So when we have == between generic types T and T, the best overload is found, given what constraints are carried by T (there's a special rule that it will never box a value-type for this (which would give a meaningless result), hence there must be some constraint guaranteeing it's a reference type). In your Follow Up 2, if you come in with DerivedTest objects, and DerivedTest derives from Test but introduces a new overload of ==, you will have the "problem" again. Which overload is called, is "burned" into the IL at compile-time.Ole
wierdly this seems to work for general reference types (where you would expect this comparison to be on reference equality) but for strings it seems to also use reference equality - so you can end up comparing 2 identical strings and having == (when in a generic method with the class constraint) say they are different.Storybook
It's because the signature doesn't match -- your constraint is for class, which effectively means that, at compile time, these are both instances of type object -- your == operator requires that both operands be of type Test, so, that operator won't be called due to a signature mismatch (remember, method and operator signature matching happens at compile time!) -- Same thing would happen if you boxed an instance of the Test class, btw. (i.e. Test a = new Test(); object b = new Test(); Console.WriteLine(a == b); will always return false.)Oleaginous
O
50

In general, EqualityComparer<T>.Default.Equals should do the job with anything that implements IEquatable<T>, or that has a sensible Equals implementation.

If, however, == and Equals are implemented differently for some reason, then my work on generic operators should be useful; it supports the operator versions of (among others):

  • Equal(T value1, T value2)
  • NotEqual(T value1, T value2)
  • GreaterThan(T value1, T value2)
  • LessThan(T value1, T value2)
  • GreaterThanOrEqual(T value1, T value2)
  • LessThanOrEqual(T value1, T value2)
Orvilleorwell answered 24/12, 2008 at 10:18 Comment(2)
"If, however, == and Equals are implemented differently for some reason" - Holy smokes! What a however! Maybe I just need to see a use case to the contrary, but a library with divergent equals semantics will likely run into bigger problems than trouble with generics.Unbecoming
@EdwardBrey you're not wrong; it would be nice if the compiler could enforce that, but...Orvilleorwell
A
48

So many answers, and not a single one explains the WHY? (which Giovanni explicitly asked)...

.NET generics do not act like C++ templates. In C++ templates, overload resolution occurs after the actual template parameters are known.

In .NET generics (including C#), overload resolution occurs without knowing the actual generic parameters. The only information the compiler can use to choose the function to call comes from type constraints on the generic parameters.

Avoid answered 12/12, 2010 at 20:38 Comment(20)
but why can't compiler treat them as a generic object? after all == works for all types be it reference types or value types. That should be the question to which I dont think you answered.Hafner
@nawfal: Actually no, == doesn't work for all value types. More importantly, it doesn't have the same meaning for all types, so the compiler doesn't know what to do with it.Avoid
Ben, oh yes I missed the custom structs we can create without any ==. Can you include that part too in your answer as I guess that's the main point hereHafner
I understand why this design decision was made. However, I'd argue that a better approach would be to try to call the most-derived == operator possible, and simply throw a MissingMethodException or something like that, if that situation is ever encountered, as it's probably an extreme minority, and is otherwise non-intuitive.Invention
Another potential solution at compile-time would be to make it a suppressable error, since, in a non-library project, the expected types are likely known, but might not be representable by a type filter (I have exactly this situation). So, I had to implement closed generic interfaces for all known Ts, which was a lot of code duplication, and nearly eliminates the utility of the generic in the first place. Plus, all the duplicated code is identical, but mandatory, because the == operator cannot take a type parameter.Invention
@dodexahedron: No, it can't be suppressed, because the compiler fails to find any function to call. It can't compile the code.Avoid
@BenVoigt The actual problem with what I suggested is that it throws away the generic behavior. What would be more useful, I think, is for type parameter filters to have a way to specify an OR list of possible types, rather than only the AND, as it currently is. So many of these would be solved by being able to use a filter like where T : not null | struct or something, since the struct or not struct limitation is what makes this such a problem so often. Then the fallback would be ValueType.Equals, which would be compilable and throw an exception at run time if mismatched, wouldn't it?Invention
@BenVoigt and, so long as the struct paths are tested first, boxing would still be avoided, so long as an == exists with both the left and right types. But that's bordering on multiple inheritance, which C# just doesn't really do. However, I don't think that isolated application of not-really-multiple-inheritance would require significant architectural changes, since the same effect is achieved with pattern matching on types, after boxing. This would just be a way of avoiding the boxing.Invention
@dodexahedron: If you want ValueType.Equals you can just call it yourself. But the == operator never maps automatically to ValueType.Equals, so it would be extremely weird for it to do so only in generic context.Avoid
@BenVoigt Do I misunderstand this document, then? learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/… \ By my reading, anyway, a non-blittable struct already does do that, by reflection, for the compiler-generated equality, no? Blittable structs are just by each field value, so they of course don't take that path. But, clearly, the code path does exist, or else this warning wouldn't exist. I've run into that stack overflow situation when a record struct type in a project became non-blittable after a change, which was non-obvious to track down.Invention
@dodexahedron: You misunderstand. That page doesn't say there is a compiler-generated operator==. It describes the behavior of passing instances of a struct to Object.Equals().Avoid
Just adding on here in case someone stumbles onto the conversation. It is correct to say that, by default, a regular user-defined struct does not implement operator==, and you must overload it yourself to get it for a struct. However, record struct types do implement it and you cannot override it. That implementation does use ValueType.Equals, and achieves it by reflection (yikes!). That was the source of the confusion. record structs behave differently out of the box, in that way.Invention
It's specifically called out here: learn.microsoft.com/en-us/dotnet/csharp/language-reference/…. Specifically, each member of the record struct is compared. If they are value types, the generated code will use ValueType.EqualsInvention
Gah. I keep taking too long to add my thoughts... It makes sense that the generated operator== for record structs calls ValueType.Equals on value type members, since those members are not guaranteed to have an operator==, but WILL have at least an implicit ValueType.Equals they inherit from ValueType.Invention
Many structs are not designed to work with ValueType.Equals. Your proposal creates a pit of failure (everything compiles but then it fails to invoke the correct comparison). My first comment, which observed that == doesn't have the same meaning for all types, is just as valid today as ever.Avoid
Exactly why it was a problem. But that's the way they behave. So says the spec, and it jives with the exceptions I received with a generic record struct. It seems like a rather unfortunate decision that the operator can't be overridden for a record struct. One can override IEquatable<T>, but, as I found out, implementing the open generic for that results in it never being called, since the compiler resolves it at compile-time, and object is a better match than T. That's why, to get the desired behavior and avoid boxing, one has to implement closed generics, so an exact type is resolved. :(Invention
It's not a proposal. It's just the way a record struct works. 🤷‍♂️ Armed with that knowledge, it is actually quite easy to replicate the same exception, which shows a stack trace including ValueType.Equals being invoked by the compiler-generated code. Again, this is for record struct types only. Normal structs don't do this unless you explicitly call .Equals on them.Invention
You're 100% correct on the behavior of a normal struct, with it never being an implicit thing...until you make that struct a member of a record struct. Then that assumption gets tossed out the window, which is not obvious without having either encountered the problem or read the doc on that behavior. I dislike it, because it was different than how a struct behaves in every situation except that one.Invention
Ah, I think you were probably referring to my earlier comment from last month, yes? Personally, I prefer a solution that has an obvious failure mode. The inconsistency created by the behavior of record structs can be resolved multiple different ways. Since structs already get an implicit ValueType.Equals that compares each member by reflection, what would be the harm in also having a default operator== that simply calls that method? It's of course only one of many ways to do it, but it seems intuitive, at least to me.Invention
Actually I was referring to this one in 2013. And one of the leading theories of language design is "fail early". So no "solution" with common failure modes, just refuse to accept the code that's likely to fail.Avoid
B
17

The compile can't know T couldn't be a struct (value type). So you have to tell it it can only be of reference type i think:

bool Compare<T>(T x, T y) where T : class { return x == y; }

It's because if T could be a value type, there could be cases where x == y would be ill formed - in cases when a type doesn't have an operator == defined. The same will happen for this which is more obvious:

void CallFoo<T>(T x) { x.foo(); }

That fails too, because you could pass a type T that wouldn't have a function foo. C# forces you to make sure all possible types always have a function foo. That's done by the where clause.

Bleachers answered 24/12, 2008 at 6:42 Comment(3)
Hosam, i tested with gmcs (mono), and it always compares references. (i.e it doesn't use an optionally defined operator== for T)Bleachers
There is one caveat with this solution: the operator== cannot be overloaded; see this StackOverflow question.Pentarchy
@DimitriC.: You totally misrepresented that other question (even though you're the one who wrote it). It doesn't show that operator== cannot be overloaded (in fact it can). It shows only that generic functions can't find operator== overloads.Avoid
T
12

It appears that without the class constraint:

bool Compare<T> (T x, T y) where T: class
{
    return x == y;
}

One should realize that while class constrained Equals in the == operator inherits from Object.Equals, while that of a struct overrides ValueType.Equals.

Note that:

bool Compare<T> (T x, T y) where T: struct
{
    return x == y;
}

also gives out the same compiler error.

As yet I do not understand why having a value type equality operator comparison is rejected by the compiler. I do know for a fact though, that this works:

bool Compare<T> (T x, T y)
{
    return x.Equals(y);
}
Thimbu answered 24/12, 2008 at 6:58 Comment(5)
u know im a total c# noob. but i think it fails because the compiler doesn't know what to do. since T isn't known yet, what is done depends on the type T if value types would be allowed. for references, the references are just compared regardless of T. if you do .Equals, then .Equal is just called.Bleachers
but if you do == on a value type, the value type doesn't have to necassary implement that operator.Bleachers
That'd make sense, litb :) It is possible that user-defined structs do not overload ==, hence the compiler fail.Thimbu
The first compare method does not use Object.Equals but instead tests reference equality. For example, Compare("0", 0.ToString()) would return false, since the arguments would be references to distinct strings, both of which have a zero as their only character.Dualism
Minor gotcha on that last one- you haven't restricted it to structs, so a NullReferenceException could happen.Trinary
O
8

You can do this with C# 11 and .NET 7+:

    static void Main()
    {
        Console.WriteLine(Compare(2, 2));
        Console.WriteLine(Compare(2, 3));
    }
    static bool Compare<T>(T x, T y) where T : IEqualityOperators<T, T, bool>
    {
        return x == y;
    }

(you may prefer to use where T : INumber<T>, which covers this scenario and a lot more, but it depends on your specific needs; not all equatable types are numbers)

Orvilleorwell answered 14/12, 2022 at 9:48 Comment(5)
Thanks! This is the Generic math support feature in C# 11, isn't it? Does it work with .NET 6?Heartwood
@HosamAly yes, but it relies upon implementation in the actual primitive types, which .net 6 lacks; it'll work for your own types and interfaces, but not for the inbuilt types, which really means "no it won't work"Orvilleorwell
Very clear. Thanks, @Marc!Heartwood
What if your generic Type T is not an INumber? How would this work? Given its generic and hasn't been constrained otherwise, we don't know what kind of type it is. How would this work for e.g. string, or user-defined reference class POCO types?Grimsley
This is great and it almost worked for me. My generic is on a base entity class and is used for the data type of the Id column which is on every table but which could be int or Guid. Then the generic repository uses it in LINQ expressions. All the examples with EqualityComparer, etc don't work because EF can't translate them. They compile but fail at runtime. This approach here almost worked, except IEqualityOperators in the constraint now makes it so I can't do a=>a.Id == id. I get 'an expression tree may not contain access of a static virtual or abstract interface member'Discordancy
C
7

Well in my case I wanted to unit-test the equality operator. I needed call the code under the equality operators without explicitly setting the generic type. Advises for EqualityComparer were not helpful as EqualityComparer called Equals method but not the equality operator.

Here is how I've got this working with generic types by building a LINQ. It calls the right code for == and != operators:

/// <summary>
/// Gets the result of "a == b"
/// </summary>
public bool GetEqualityOperatorResult<T>(T a, T b)
{
    // declare the parameters
    var paramA = Expression.Parameter(typeof(T), nameof(a));
    var paramB = Expression.Parameter(typeof(T), nameof(b));
    // get equality expression for the parameters
    var body = Expression.Equal(paramA, paramB);
    // compile it
    var invokeEqualityOperator = Expression.Lambda<Func<T, T, bool>>(body, paramA, paramB).Compile();
    // call it
    return invokeEqualityOperator(a, b);
}

/// <summary>
/// Gets the result of "a =! b"
/// </summary>
public bool GetInequalityOperatorResult<T>(T a, T b)
{
    // declare the parameters
    var paramA = Expression.Parameter(typeof(T), nameof(a));
    var paramB = Expression.Parameter(typeof(T), nameof(b));
    // get equality expression for the parameters
    var body = Expression.NotEqual(paramA, paramB);
    // compile it
    var invokeInequalityOperator = Expression.Lambda<Func<T, T, bool>>(body, paramA, paramB).Compile();
    // call it
    return invokeInequalityOperator(a, b);
}
Cocteau answered 3/9, 2016 at 9:39 Comment(2)
It's bad form to have Equals() not behave the same as ==. Aside from that, though, this seems like a run-time expensive solution. If the expected set of types for T is not large, but a type parameter filter isn't possible (such as when T can be value and reference types), explicitly implementing the closed generic interfaces, while tedious, results in better runtime performance, more intuitive behavior, and sometimes even a smaller compiled assembly than the open generic (always, in my experience, but I'm not going to assert a definite here).Invention
I had considered a solution almost exactly like this when trying to solve this problem, but bit the bullet and implemented IComparable<T> for every type the application actually accepts/uses, and it's mostly just a copy/paste job, since the equality/inequality operators are identical except for the types in their signatures. It feels dirty, but, if the expected set of types is known, it's perfectly valid. I would love to have an alternative, though, such as type filters as a list, or even just generics resolving at runtime and throwing an exception at runtime if equality isn't defined.Invention
C
6

There is an MSDN Connect entry for this here

Alex Turner's reply starts with:

Unfortunately, this behavior is by design and there is not an easy solution to enable use of == with type parameters that may contain value types.

Concubinage answered 19/8, 2009 at 15:51 Comment(0)
C
5

If you want to make sure the operators of your custom type are called you can do so via reflection. Just get the type using your generic parameter and retrieve the MethodInfo for the desired operator (e.g. op_Equality, op_Inequality, op_LessThan...).

var methodInfo = typeof (T).GetMethod("op_Equality", 
                             BindingFlags.Static | BindingFlags.Public);    

Then execute the operator using the MethodInfo's Invoke method and pass in the objects as the parameters.

var result = (bool) methodInfo.Invoke(null, new object[] { object1, object2});

This will invoke your overloaded operator and not the one defined by the constraints applied on the generic parameter. Might not be practical, but could come in handy for unit testing your operators when using a generic base class that contains a couple of tests.

Cottar answered 18/2, 2011 at 14:25 Comment(0)
E
4

I wrote the following function looking at the latest msdn. It can easily compare two objects x and y:

static bool IsLessThan(T x, T y) 
{
    return ((IComparable)(x)).CompareTo(y) <= 0;
}
Exequatur answered 20/6, 2013 at 2:3 Comment(4)
You can get rid of your booleans and write return ((IComparable)(x)).CompareTo(y) <= 0;Zoilazoilla
What if T isn't an IComparable? Won't this throw an invalid cast exception? And if T is an IComparable why not just add that to the generic type constraint?Coverall
@Coverall IComparable is an interface.Exequatur
@Exequatur yes, I'm aware. My concern is that nothing guarantees T actually implements that interface. If it doesn't, then you get an exception. If it does, then it should probably be part of the generic constraint.Coverall
S
3

bool Compare(T x, T y) where T : class { return x == y; }

The above will work because == is taken care of in case of user-defined reference types.
In case of value types, == can be overridden. In which case, "!=" should also be defined.

I think that could be the reason, it disallows generic comparison using "==".

Strachey answered 24/12, 2008 at 6:43 Comment(1)
The == token is used for two different operators. If for the given operand types there exists a compatible overload of the equality operator, that overload will be used. Otherwise if both operands are reference types that are compatible with each other, a reference comparison will be used. Note that in the Compare method above the compiler can't tell that the first meaning applies, but can tell the second meaning applies, so the == token will use the latter even if T overloads the equality-check operator (e.g. if it's type String).Dualism
S
1

The .Equals() works for me while TKey is a generic type.

public virtual TOutputDto GetOne(TKey id)
{
    var entity =
        _unitOfWork.BaseRepository
            .FindByCondition(x => 
                !x.IsDelete && 
                x.Id.Equals(id))
            .SingleOrDefault();


    // ...
}
Stinkhorn answered 18/10, 2019 at 6:16 Comment(1)
That's x.Id.Equals, not id.Equals. Presumably, the compiler knows something about the type of x.Heartwood
B
1

I have 2 solutions and they're very simply.

Solution 1: Cast the generic typed variable to object and use == operator.

Example:

void Foo<T>(T t1, T t2)
{
   object o1 = t1;
   object o2 = t2;
   if (o1 == o2)
   {
      // ...
      // ..
      // . 
   }
}

Solution 2: Use object.Equals(object, object) method.

Example:

void Foo<T>(T t1, T t2)
{
   if (object.Equals(t1, t2)
   {
       // ...
       // ..
       // .
   }
}
Blotto answered 20/8, 2021 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.