Search code examples
c#referenceequals

Comparing objects with same type evaluates for false


I have this code in an if statements which evaluates to false and I don't have any clue why

&& (typeof(TResponse).Equals(typeof(MediatorResponse)))

used Equals as I intend to compare by reference

I tried to put them in watch and this is my clue

click to see

typeof(TResponse) seems to be a MediatorResponse'1 while typeof(MediatorResponse) seems to be a MediatorResponse are these 2 still of the same type?

Why does visual studio put 1 on the other?


Solution

  • C# allows you to have different types with the same name if one or more and generic, and if the generic types have a different number of generic arguments. For example, the following are all different types:

    class Foo{}
    class Foo<T>{}
    class Foo<T1, T2>{}
    

    The backtick character followed by a number is used on generic types to generate a unique name, and indicate the number of generic arguments the type has. So in the above example the names would be:

    class Foo{}            // Name is Foo
    class Foo<T>{}         // Name is Foo`1
    class Foo<T1, T2>{}    // Name is Foo`2