{"id":110677,"date":"2024-12-25T07:00:00","date_gmt":"2024-12-25T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=110677"},"modified":"2024-12-24T06:52:58","modified_gmt":"2024-12-24T14:52:58","slug":"20241225-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20241225-00\/?p=110677","title":{"rendered":"How can I check if two GUIDs are equal when they are provided as strings?"},"content":{"rendered":"<p>A customer asked if there was a helper function in the system that accepted two strings and reported whether the <code>GUID<\/code>s they represent are equal.<\/p>\n<p>This is a tricky question, because you first have to decide what &#8220;represent&#8221; means.<\/p>\n<p>There are many ways of representing a <code>GUID<\/code> as a string. It could be just 32 case-insensitive hexadecimal digits. Or maybe there are internal hyphens to separate the groups. And the whole thing might be enclosed in braces or parentheses. External and interior whitespace might be allowed. Trailing garbage might be accepted (and ignored). And then there&#8217;s the special format <tt>{0x00000000,<wbr \/>0x0000,<wbr \/>0x0000,<wbr \/>{0x00,<wbr \/>0x00,<wbr \/>0x00,<wbr \/>0x00,<wbr \/>0x00,<wbr \/>0x00,<wbr \/>0x00,<wbr \/>0x00}}<\/tt> supported by <code>System.Guid.Parse<\/code>. That last one is tricky because it means you can&#8217;t just use a na\u00efve algorithm of just discarding anything that is not a hexadecimal digit.<\/p>\n<p>The simplest solution is to pass both strings to a <code>GUID<\/code> parser that supports the formats that you want to support, then compare the resulting <code>GUID<\/code>s. You could write something fancy (say, ignore anything that&#8217;s not a hexadecimal digit, or the sequence <tt>0x<\/tt>), but the straightforwardness of just deferring to another parser likely outweighs the risk that your custom <code>GUID<\/code> comparison function will mess something up.<\/p>\n<p>If comparing <code>GUID<\/code>-holding strings is a bottleneck in your code, then the solution is not to improve your &#8220;comparing two <code>GUID<\/code>-holding strings&#8221; function, but rather to change your code so that <code>GUID<\/code>s are passed as <code>GUID<\/code>s.\u00b9<\/p>\n<p>\u00b9 If the string arrives from an external source (say, JSON), then convert it to a <code>GUID<\/code> immediately upon parsing the JSON, and then use the parsed value from then on.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer asked if there was a helper function in the system that accepted two strings and reported whether the GUIDs they represent are equal. This is a tricky question, because you first have to decide what &#8220;represent&#8221; means. There are many ways of representing a GUID as a string. It could be just 32 [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-110677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer asked if there was a helper function in the system that accepted two strings and reported whether the GUIDs they represent are equal. This is a tricky question, because you first have to decide what &#8220;represent&#8221; means. There are many ways of representing a GUID as a string. It could be just 32 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=110677"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110677\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=110677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=110677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=110677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}