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

 

posted @ 2020-06-18 15:10  ChuckLu  阅读(293)  评论(0编辑  收藏  举报