473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

System.Uri & hashtables: Is it a bug?

Hello,

The following code writes the text string:

Uri uri1 = new Uri("http://www.here.net/aplace?param=one");
Uri uri2 = new Uri("http://www.here.net/aplace?param=two");
int h1 = uri1.GetHashCode();
int h2 = uri2.GetHashCode();
bool e = uri1.Equals(uri2);
if (e == true && h1 != h2)
{
System.Console.WriteLine("Is is a bug or is it a feature?");
}

I thought the hash codes must be the same when two instances are equal. BTW,
I found this because Uri's seem to be unusable as keys in a hashtable.

I am using the following versions:
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322

Cheers,
Michael
Nov 15 '05 #1
6 2264

"Michael Schollmeyer" <ms*****@web.de> wrote in message
news:bp**********@svr7.m-online.net...
Hello,

The following code writes the text string:

Uri uri1 = new Uri("http://www.here.net/aplace?param=one");
Uri uri2 = new Uri("http://www.here.net/aplace?param=two");
int h1 = uri1.GetHashCode();
int h2 = uri2.GetHashCode();
bool e = uri1.Equals(uri2);
if (e == true && h1 != h2)
{
System.Console.WriteLine("Is is a bug or is it a feature?");
}

I thought the hash codes must be the same when two instances are equal. BTW, I found this because Uri's seem to be unusable as keys in a hashtable.

I am using the following versions:
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322

Cheers,
Michael


That's unexpected. The docs for Uri.Equals explain that it doesn't consider
fragments, but doesn't say anything about ignoring query strings. If you
rely on Equals to consider query data, you can certainly create a derived
class to provide the desired behavior:

class MyUri : Uri
{
public MyUri(string uri) : base(uri)
{
}

public override bool Equals(object comparand)
{
if (!base.Equals(comparand))
return false;

Uri uri = comparand as Uri;
if (uri == null)
return false;

return this.PathAndQuery == uri.PathAndQuery;
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}

This doesn't work quite like Uri.Equals in the way it handles string
arguments, but you can extend it however you like.
Nov 15 '05 #2
Michael Schollmeyer <ms*****@web.de> wrote:
The following code writes the text string:

Uri uri1 = new Uri("http://www.here.net/aplace?param=one");
Uri uri2 = new Uri("http://www.here.net/aplace?param=two");
int h1 = uri1.GetHashCode();
int h2 = uri2.GetHashCode();
bool e = uri1.Equals(uri2);
if (e == true && h1 != h2)
{
System.Console.WriteLine("Is is a bug or is it a feature?");
}

I thought the hash codes must be the same when two instances are equal. BTW,
I found this because Uri's seem to be unusable as keys in a hashtable.


Yes, that looks like a bug to me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

"Michael Schollmeyer" <ms*****@web.de> wrote in message
news:bp**********@svr7.m-online.net...
Hello,

The following code writes the text string:

Uri uri1 = new Uri("http://www.here.net/aplace?param=one");
Uri uri2 = new Uri("http://www.here.net/aplace?param=two");
int h1 = uri1.GetHashCode();
int h2 = uri2.GetHashCode();
bool e = uri1.Equals(uri2);
if (e == true && h1 != h2)
{
System.Console.WriteLine("Is is a bug or is it a feature?");
}

I thought the hash codes must be the same when two instances are equal. BTW, I found this because Uri's seem to be unusable as keys in a hashtable.

I am using the following versions:
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322

Cheers,
Michael


That's unexpected. The docs for Uri.Equals explain that it doesn't consider
fragments, but doesn't say anything about ignoring query strings. If you
rely on Equals to consider query data, you can certainly create a derived
class to provide the desired behavior:

class MyUri : Uri
{
public MyUri(string uri) : base(uri)
{
}

public override bool Equals(object comparand)
{
if (!base.Equals(comparand))
return false;

Uri uri = comparand as Uri;
if (uri == null)
return false;

return this.PathAndQuery == uri.PathAndQuery;
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}

This doesn't work quite like Uri.Equals in the way it handles string
arguments, but you can extend it however you like.
Nov 15 '05 #4
Hmmm i'm not really sure what the mentality or some of the arguments
for/against the querystring being considered as part of the hashcode, but
I'm sure there's a good reason...

Bret's suggestion should work for you if need be...

--
Eric Newton
C#/ASP Application Developer
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael Schollmeyer <ms*****@web.de> wrote:
The following code writes the text string:

Uri uri1 = new Uri("http://www.here.net/aplace?param=one");
Uri uri2 = new Uri("http://www.here.net/aplace?param=two");
int h1 = uri1.GetHashCode();
int h2 = uri2.GetHashCode();
bool e = uri1.Equals(uri2);
if (e == true && h1 != h2)
{
System.Console.WriteLine("Is is a bug or is it a feature?");
}

I thought the hash codes must be the same when two instances are equal. BTW, I found this because Uri's seem to be unusable as keys in a hashtable.


Yes, that looks like a bug to me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
Michael Schollmeyer wrote:
Hello,

The following code writes the text string:

Uri uri1 = new Uri("http://www.here.net/aplace?param=one");
Uri uri2 = new Uri("http://www.here.net/aplace?param=two");
int h1 = uri1.GetHashCode();
int h2 = uri2.GetHashCode();
bool e = uri1.Equals(uri2);
if (e == true && h1 != h2)
{
System.Console.WriteLine("Is is a bug or is it a feature?");
}

I thought the hash codes must be the same when two instances are equal. BTW,
I found this because Uri's seem to be unusable as keys in a hashtable.

This looks like a bug.

Uri.Equals() seems to key off of the Scheme, HostType (host name or IP
addr), Port (only in Framework 1.1) and AbsolutePath properties.

Uri.GetHashCode() derives its result from the AbsoluteUri property.

I am using the following versions:
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322

Cheers,
Michael


--
mikeb

Nov 15 '05 #6
Hello Bret~

I am using a hashtable that maps instances or Uri to objects. I cannot
derive the key's type from Uri because the object construction is outside my
code domain.

I also believe that there is a more general problem with using instances of
Uri as keys into hashtables. Look at the following code:

Uri u0 = new Uri("http://www.here.net/adoc?param=12a7665f334edd32");
Uri u1 = new Uri("http://www.here.net/adoc?param=1543ffefa23c34b5");
Uri u2 = new Uri("http://www.here.net/adoc?param=68e20c473edb271d");
Hashtable h = new Hashtable();
h[u0] = 1;
h[u1] = 2;
h[u2] = 3;
int a = (int)h[u0];
int b = (int)h[u1];
int c = (int)h[u2];
int count = h.Count;

Run the code and look at the values for a, b, c, and count. You will be more
than surprised!
BTW, the numbers I've chosen after param= are NOT random...

IMHO, people should be discouraged from using Uri as keys into hashtables
without explicitly specifying an IHashCodeProvider because the current
implementation of Uri breaks a vital rule: When o1.Equals(o2) is true, then
o1.GetHashCode() MUST be equal to o2.GetHashCode(). Everything else is a
bug.
"Bret Mulvey [MS]" <br***@online.microsoft.com> wrote in message
news:AC6tb.1328$Dw6.12421@attbi_s02...

"Michael Schollmeyer" <ms*****@web.de> wrote in message
news:bp**********@svr7.m-online.net...
Hello,

The following code writes the text string:

Uri uri1 = new Uri("http://www.here.net/aplace?param=one");
Uri uri2 = new Uri("http://www.here.net/aplace?param=two");
int h1 = uri1.GetHashCode();
int h2 = uri2.GetHashCode();
bool e = uri1.Equals(uri2);
if (e == true && h1 != h2)
{
System.Console.WriteLine("Is is a bug or is it a feature?");
}

I thought the hash codes must be the same when two instances are equal. BTW,
I found this because Uri's seem to be unusable as keys in a hashtable.

I am using the following versions:
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322

Cheers,
Michael


That's unexpected. The docs for Uri.Equals explain that it doesn't

consider fragments, but doesn't say anything about ignoring query strings. If you
rely on Equals to consider query data, you can certainly create a derived
class to provide the desired behavior:

class MyUri : Uri
{
public MyUri(string uri) : base(uri)
{
}

public override bool Equals(object comparand)
{
if (!base.Equals(comparand))
return false;

Uri uri = comparand as Uri;
if (uri == null)
return false;

return this.PathAndQuery == uri.PathAndQuery;
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}

This doesn't work quite like Uri.Equals in the way it handles string
arguments, but you can extend it however you like.

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Maxim Kazitov | last post by:
Hi, I create application which transform huge XML files (~ 150 Mb) to CVS files. And I am facing strange problem. First 1000 rows parsed in 1 sec after 20000 rows speed down to 100 rows per sec,...
13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
4
by: Anders Borum | last post by:
Hello! I have a list of singleton classes (model managers) that store objects internally using hashtables. Each of these classes use a single hashtable to store e.g. users, pages, elements and...
1
by: Curtis | last post by:
Does anyone know the proper method to save information to embedded hashtables. I am trying to save parent/child information to hashtables but I am not getting the correct results I have made a...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
7
by: Kamran Shafi | last post by:
Hi, I am creating an arraylist (say masterArrayList) of hashtables, where each hashtable (say table) is of the format key=string, value = arraylist of strings (say existing_strings). In a...
7
by: Mike P | last post by:
I have 2 hashtables each of which I am using to store a set of IDs and Descriptions. Hashtable 1 will have the full set of data (e.g. IDs 1,2,3,4,5) whereas Hashtable 2 will only have a subset of...
0
by: John Smith | last post by:
Hello people, I have a performance query regarding LINQ that I would like some opinions. Currently we have a business logic framework that is used in n-tier applications. We read data from a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.