The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog

Syndication

Search

Tags

News





  • View Maurice De Beijer's profile on LinkedIn

Community

Email Notifications

Explore

Archives

Comparing two Uri’s

This one bit me today a bit unexpectedly.

 

I was comparing two Uri’s and was getting unexpected matches between two Uri’s that where quite clearly not the same. Turns out the when comparing Uri’s the Fragment, or anchor or part after the #, is not part of the comparison.

So the following unit test passes even though I would have expected it to fail!

[TestMethod]
public void TestTwoEqualUrisWithDifferentAnchorShouldNotBeEqual()
{
    var baseUri = new Uri("http://www.test.com");
    var uri1 = new Uri(baseUri, "#anchor1");
    var uri2 = new Uri(baseUri, "#anchor2");
    
    Assert.AreEqual(uri1, uri2);
    Assert.AreNotEqual(uri1.ToString(), uri2.ToString());
}

 

Note that when comparing uri1 and uri2 they are equal even though they use two different anchor tags. Comparing the ToString() results does show the difference though. Wasted another bit of time on this piece of trivia Sad

 

www.TheProblemSolver.nl
Wiki.WindowsWorkflowFoundation.eu

Published Fri, Nov 13 2009 21:46 by Maurice
Filed under: