C# Type Comparison: Type.Equals vs operator ==
I suggest that you read the excellent When is a Type not a Type? blog post by Brad Wilson. To summarize: a runtime type (represented by the internal type RuntimeType), managed by the CLR, is not always the same as a Type
, which can be extended. Equals
will check the underlying system type, whereas ==
will check the type itself.
A simple example:
Type type = new TypeDelegator(typeof(int));
Console.WriteLine(type.Equals(typeof(int))); // Prints True
Console.WriteLine(type == typeof(int)); // Prints False
The reason is simple: The two are functionally equivalent in this case and the latter is more readable.