MyGuid.Equals(OtherGuid) 不等于?
当我使用 MyGuid.ToString().Equals(OtherGuid.ToString()) 时,
它们是 Equal,为什么当我比较纯 Guid 时它们不相等?
更新:
这里的问题可能是我使用了第三方控件。
下面的 .Key 有一个 Guid,committeeId 也是一个 Guid。只有当我
对两个 Guid 执行 ToString() 时,它们才相等,这很奇怪。
for (int i = 0; i < this.ultraCalendarInfo.Owners.Count; i++)
if (ultraCalendarInfo.Owners[i].Key.ToString().Equals(committeeId))
ultraCalendarInfo.Owners[i].Visible = isVisible;
When I use MyGuid.ToString().Equals(OtherGuid.ToString())
they are Equal, why are they not equal when I compare the pure Guid ?
Update:
Well the problem here could be that I use a 3rd party control.
The .Key below has a Guid and committeeId is a Guid too. They were never Equal only when I did
ToString() on both Guid`s they were equal thats odd.
for (int i = 0; i < this.ultraCalendarInfo.Owners.Count; i++)
if (ultraCalendarInfo.Owners[i].Key.ToString().Equals(committeeId))
ultraCalendarInfo.Owners[i].Visible = isVisible;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法重现该问题:
Produces:
请给出一个类似的简短但完整的程序来演示该问题。
编辑:我想我现在看到了问题,它在你的代码中:
你已经说过:
您在 Guid 上调用
ToString()
,但在committeeId
上未调用,因此该条件永远不会成立。如果您对两者或两者都不调用ToString()
,应该没问题。我强烈怀疑这个(或者非常类似的东西,如果上面不是你的真实代码)就是问题所在。当然,多次调用
ToString()
(即guid.ToString().ToString()
等)总是会返回相同的字符串 - 所以如果你有一个不平衡的字符串ToString()
调用次数(一侧 0,另一侧 1),然后向两侧添加额外的调用将“修复”问题...但删除ToString() 调用之一是真正的修复方法。I can't reproduce the problem:
Produces:
Please give a similar short but complete program which demonstrates the problem.
EDIT: I think I see the problem now, and it's in your code:
You've stated:
You're calling
ToString()
on the Guid but not oncommitteeId
, so that condition will never be true. If you calledToString()
on both or neither it should be fine.I strongly suspect that this (or something very similar, if the above isn't your real code) is the problem. Calling
ToString()
more than once (i.e.guid.ToString().ToString()
etc) will always return the same string, of course - so if you have an unbalanced number ofToString()
calls (0 on one side and 1 on the other) then adding an extra call to both sides would "fix" the problem... but removing one of theToString()
calls is the real fix.考虑下面的代码
将 GUID 存储在“对象”类型的变量中具有将其“装箱”到引用类型中的效果。当使用“==”比较引用类型时,即使它们包含的值相等,它们也可能不相等。这与值类型不同,因为如果您将上面的 g1 和 g2 声明为 Guid,那么所有相等测试都会得到 True。但上面的代码对于“==”测试返回 False。请注意,如果您“取消装箱”第三个测试中所示的值,它将看到这些值相等。如果您使用“Equals”方法,它还会发现它们是相等的,该方法可以(并且已经)被重写,以根据对象的特定类型对对象执行更智能的比较。
Consider the code below
Storing a GUID in a variable of type "object" has the effect of "boxing" it into a reference type. When you use "==" to compare reference types, they may not be equal even if the values contained in them are equal. This is a difference from value types because if you were to declare g1 and g2 above as Guid, then you would get True for all the equality tests. But the above code returns False for the "==" test. Notice that if you "un-box" the values as shown in the third test, it will see that the values are equal. It will also see that they are equal if you use the "Equals" method, which can be (and is) overridden to perform more intelligent comparisons of objects based on their specific types.
由于您没有显示两个 guid 的声明,我唯一能想到的是它们是不同的 guid 类型(我在这里使用 SqlGuid 作为示例):
As you don't show the declaration of the two guids, the only thing I can think of is that they are different guid types (I'm using SqlGuid as an example here):