C# Type Comparison: Type.Equals vs operator ==
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