c#:动作无与伦比?
我正在尝试比较两个操作。与 == 的比较总是返回 false,就像 Equals 方法一样,即使它是同一个实例。
我的问题是:这真的不可能还是我做错了?
干杯 交流电
I'm trying to compare two Actions. The comparison with == always returns false as does the Equals-method even though it's the same instance.
My question is: Is it really not possible or am I doing it wrong?
Cheers
AC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你做错了。
如果我相信你,当你说“即使它是同一个实例”时,那么通过 LINQPad 告诉我你一定做错了什么,或者“同一个实例”不正确:
输出是:
换句话说,都是
==
,a.Equals(b)
和object.ReferenceEquals(a, b)
表示它是同一个实例。另一方面,如果我复制代码:
那么它们都会报告错误。
如果我将它们都链接到一个命名方法,而不是匿名方法:
那么输出是:
换句话说,我现在得到了两个
Action
实例,而不仅仅是一个,但它们仍然比较相等。You are doing it wrong.
If I am to believe you, when you say "even though it's the same instance", then the following code executed through LINQPad tells me that you must be doing something wrong, or the "same instance" is incorrect:
The output is:
In other words, both
==
,a.Equals(b)
andobject.ReferenceEquals(a, b)
says its the same instance.On the other hand, if I duplicate the code:
Then they all report false.
If I link them both to a named method, and not an anonymous one:
Then the output is:
In other words, I now got two
Action
instances, not just one, but they still compare equal.您可以比较
Method
和Target
属性。You can compare
Method
andTarget
properties.您可以比较
action.Method
和action.Target
。You can compare
action.Method
andaction.Target
.