C# 类型比较:Type.Equals 与运算符 ==

发布于 2025-01-04 06:38:30 字数 1269 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

っ〆星空下的拥抱 2025-01-11 06:38:30

我建议您阅读优秀的 何时类型不是类型? Brad Wilson 的博客文章。总结一下:由 CLR 管理的运行时类型(由内部类型 RuntimeType 表示)并不总是与可以扩展的 Type 相同。 Equals 将检查底层系统type,而 == 将检查类型本身。

一个简单的例子:

Type type = new TypeDelegator(typeof(int));
Console.WriteLine(type.Equals(typeof(int))); // Prints True
Console.WriteLine(type == typeof(int));      // Prints False

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
冬天的雪花 2025-01-11 06:38:30

原因很简单:在这种情况下,两者在功能上是等效的,并且后者更具可读性。

The reason is simple: The two are functionally equivalent in this case and the latter is more readable.

回首观望 2025-01-11 06:38:30

来自 http://blogs.msdn.com/b/csharpfaq/archive/2004/03/29/when-should-i-use-and-when-should-i-use-equals.aspx< /a>

Equals 方法只是 System.Object 中定义的一个虚拟方法,并且
被任何选择这样做的类覆盖。 == 运算符是一个
可以由类重载的运算符,但通常具有
身份行为。

对于 == 尚未重载的引用类型,它会进行比较
两个引用是否引用同一个对象 - 这正是
System.Object 中 Equals 的实现做了什么。

默认情况下,值类型不提供 == 的重载。然而,
框架提供的大多数值类型都提供了自己的值类型
超载。值类型的 Equals 默认实现是
由ValueType提供,并使用反射进行比较,
这使得它比特定类型慢得多
实施通常会是。该实现还调用
等于正在比较的两个值内的引用对。

但是,这两种比较的主要区别在于
正常使用(您不太可能定义自己的值类型
很多时候)是多态性。运算符被重载,而不是被覆盖,
这意味着除非编译器知道调用更具体的
version,它只会调用身份版本。

From http://blogs.msdn.com/b/csharpfaq/archive/2004/03/29/when-should-i-use-and-when-should-i-use-equals.aspx

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文