C# 类型比较:Type.Equals 与运算符 ==
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9234009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Type Comparison: Type.Equals vs operator ==
提问by Metro Smurf
Resharper suggests that the following be changed from:
Resharper 建议更改以下内容:
Type foo = typeof( Foo );
Type bar = typeof( Bar );
if( foo.Equals( bar ) ) { ... }
To:
到:
if( foo == bar ) { ... }
operator ==
运算符 ==
// Summary:
// Indicates whether two System.Type objects are equal.
//
// Parameters:
// left:
// The first object to compare.
//
// right:
// The second object to compare.
//
// Returns:
// true if left is equal to right; otherwise, false.
public static bool operator ==( Type left, Type right );
Equals( Type o )
等于(类型 o )
// Summary:
// Determines if the underlying system type of the current System.Type is the
// same as the underlying system type of the specified System.Type.
//
// Parameters:
// o:
// The System.Type whose underlying system type is to be compared with the underlying
// system type of the current System.Type.
//
// Returns:
// true if the underlying system type of o is the same as the underlying system
// type of the current System.Type; otherwise, false.
public virtual bool Equals( Type o );
Question
Why would operator ==be recommended over Equals( Type o )when comparing Types?
问题
为什么在比较类型时会operator ==被推荐Equals( Type o )?
采纳答案by Julien Lebosquain
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. Equalswill check the underlying system type, whereas ==will check the type itself.
我建议你阅读优秀的时候是类型不是类型?布拉德威尔逊的博客文章。总结一下:由 CLR 管理的运行时类型(由内部类型 RuntimeType 表示)并不总是与 a 相同Type,可以扩展。Equals将检查底层系统类型,而==将检查类型本身。
A simple example:
一个简单的例子:
Type type = new TypeDelegator(typeof(int));
Console.WriteLine(type.Equals(typeof(int))); // Prints True
Console.WriteLine(type == typeof(int)); // Prints False
回答by Justin Niessner
The reason is simple: The two are functionally equivalent in this case and the latter is more readable.
原因很简单:在这种情况下,两者在功能上是等效的,而后者更具可读性。
回答by Paul Nikonowicz
The Equals method is just a virtual one defined in System.Object, and overridden by whichever classes choose to do so. The == operator is an operator which can be overloaded by classes, but which usually has identity behaviour.
For reference types where == has not been overloaded, it compares whether two references refer to the same object - which is exactly what the implementation of Equals does in System.Object.
Value types do not provide an overload for == by default. However, most of the value types provided by the framework provide their own overload. The default implementation of Equals for a value type is provided by ValueType, and uses reflection to make the comparison, which makes it significantly slower than a type-specific implementation normally would be. This implementation also calls Equals on pairs of references within the two values being compared.
However, the main difference between the two types of comparison in normal use (where you're unlikely to be defining your own value types very often) is polymorphism. Operators are overloaded, not overridden, which means that unless the compiler knows to call the more specific version, it'll just call the identity version.
Equals 方法只是在 System.Object 中定义的一个虚拟方法,并被任何选择这样做的类覆盖。== 运算符是一个可以被类重载的运算符,但它通常具有标识行为。
对于 == 没有被重载的引用类型,它比较两个引用是否引用同一个对象——这正是 System.Object 中 Equals 的实现所做的。
默认情况下,值类型不为 == 提供重载。但是,框架提供的大多数值类型都提供了它们自己的重载。值类型的 Equals 的默认实现由 ValueType 提供,并使用反射进行比较,这使得它比通常的类型特定实现慢得多。此实现还对要比较的两个值中的引用对调用 Equals。
但是,在正常使用中(您不太可能经常定义自己的值类型)这两种比较类型之间的主要区别是多态性。运算符是重载的,而不是被覆盖的,这意味着除非编译器知道调用更具体的版本,否则它只会调用标识版本。

