C# 类型比较:Type.Equals 与运算符 ==
ReSharper 建议将以下内容更改为:
Type foo = typeof( Foo );
Type bar = typeof( Bar );
if( foo.Equals( bar ) ) { ... }
if( foo == bar ) { ... }
运算符 ==
// 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 )
// 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 );
问题
在比较类型时,为什么会推荐 operator ==
而不是 Equals( Type o )
?
ReSharper suggests that the following be changed from:
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 )
// 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您阅读优秀的 何时类型不是类型? Brad Wilson 的博客文章。总结一下:由 CLR 管理的运行时类型(由内部类型 RuntimeType 表示)并不总是与可以扩展的
Type
相同。Equals
将检查底层系统type,而==
将检查类型本身。一个简单的例子:
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:
原因很简单:在这种情况下,两者在功能上是等效的,并且后者更具可读性。
The reason is simple: The two are functionally equivalent in this case and the latter is more readable.
来自 http://blogs.msdn.com/b/csharpfaq/archive/2004/03/29/when-should-i-use-and-when-should-i-use-equals.aspx< /a>
From http://blogs.msdn.com/b/csharpfaq/archive/2004/03/29/when-should-i-use-and-when-should-i-use-equals.aspx