MyGuid.Equals(OtherGuid) 不等于?

发布于 2024-09-15 02:06:18 字数 503 浏览 7 评论 0原文

当我使用 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 技术交流群。

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

发布评论

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

评论(3

不顾 2024-09-22 02:06:18

我无法重现该问题:

using System;

class Program
{
    static void Main(string[] args)
    {
        Guid x = Guid.NewGuid();
        Guid y = new Guid(x.ToString());

        Console.WriteLine(x == y);
        Console.WriteLine(x.Equals(y));
        Console.WriteLine(x.ToString() == y.ToString());
    }
}

Produces:

True
True
True

请给出一个类似的简短但完整的程序来演示该问题。

编辑:我想我现在看到了问题,它在你的代码中:

if (ultraCalendarInfo.Owners[i].Key.ToString().Equals(committeeId))

你已经说过:

下面的.Key有一个Guid,committeeId也是一个Guid。

您在 Guid 上调用 ToString(),但在 committeeId调用,因此该条件永远不会成立。如果您对两者两者都不调用ToString(),应该没问题。

我强烈怀疑这个(或者非常类似的东西,如果上面不是你的真实代码)就是问题所在。当然,多次调用 ToString() (即 guid.ToString().ToString() 等)总是会返回相同的字符串 - 所以如果你有一个不平衡的字符串ToString() 调用次数(一侧 0,另一侧 1),然后向两侧添加额外的调用“修复”问题...但删除ToString() 调用之一是真正的修复方法。

I can't reproduce the problem:

using System;

class Program
{
    static void Main(string[] args)
    {
        Guid x = Guid.NewGuid();
        Guid y = new Guid(x.ToString());

        Console.WriteLine(x == y);
        Console.WriteLine(x.Equals(y));
        Console.WriteLine(x.ToString() == y.ToString());
    }
}

Produces:

True
True
True

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:

if (ultraCalendarInfo.Owners[i].Key.ToString().Equals(committeeId))

You've stated:

The .Key below has a Guid and committeeId is a Guid too.

You're calling ToString() on the Guid but not on committeeId, so that condition will never be true. If you called ToString() 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 of ToString() 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 the ToString() calls is the real fix.

岁月无声 2024-09-22 02:06:18

考虑下面的代码

object g1 = Guid.NewGuid();
object g2 = new Guid(((Guid)g1).ToByteArray());
Console.WriteLine("{0}\r\n{1}", g1, g2);
Console.WriteLine("   Equals: {0}", g1.Equals(g2));
Console.WriteLine("Object ==: {0}", g1 == g2);
Console.WriteLine(" Value ==: {0}", (Guid)g1 == (Guid)g2);

将 GUID 存储在“对象”类型的变量中具有将其“装箱”到引用类型中的效果。当使用“==”比较引用类型时,即使它们包含的值相等,它们也可能不相等。这与值类型不同,因为如果您将上面的 g1 和 g2 声明为 Guid,那么所有相等测试都会得到 True。但上面的代码对于“==”测试返回 False。请注意,如果您“取消装箱”第三个测试中所示的值,它将看到这些值相等。如果您使用“Equals”方法,它还会发现它们是相等的,该方法可以(并且已经)被重写,以根据对象的特定类型对对象执行更智能的比较。

Consider the code below

object g1 = Guid.NewGuid();
object g2 = new Guid(((Guid)g1).ToByteArray());
Console.WriteLine("{0}\r\n{1}", g1, g2);
Console.WriteLine("   Equals: {0}", g1.Equals(g2));
Console.WriteLine("Object ==: {0}", g1 == g2);
Console.WriteLine(" Value ==: {0}", (Guid)g1 == (Guid)g2);

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.

心意如水 2024-09-22 02:06:18

由于您没有显示两个 guid 的声明,我唯一能想到的是它们是不同的 guid 类型(我在这里使用 SqlGuid 作为示例):

 Sub Main()

        Dim stringValue = Guid.NewGuid().ToString()

        Dim first = New Guid(stringValue)
        Dim second = New SqlTypes.SqlGuid(stringValue)

        Console.WriteLine(first = second)    'True'
        Console.WriteLine(first.Equals(second))    'False'
        Console.WriteLine(first.ToString() = second.ToString())    'True'
        Console.ReadKey()

    End Sub

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):

 Sub Main()

        Dim stringValue = Guid.NewGuid().ToString()

        Dim first = New Guid(stringValue)
        Dim second = New SqlTypes.SqlGuid(stringValue)

        Console.WriteLine(first = second)    'True'
        Console.WriteLine(first.Equals(second))    'False'
        Console.WriteLine(first.ToString() = second.ToString())    'True'
        Console.ReadKey()

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