PC Review


Reply
Thread Tools Rate Thread

Dictionary Generic Class

 
 
kim.nolsoee@gmail.com
Guest
Posts: n/a
 
      2nd Jan 2007
Hi

I want to use the Dictionary Classs that will load my own class
called KeyClass used as TKey.

Here is the code:

public class Dictionary
{
public static void Main()
{
Dictionary<KeyClass, string> myDic = new
Dictionary<KeyClass, string>();

KeyClass kc1 = new KeyClass(new string[]{ "1", "2", "3" },
"1");
KeyClass kc2 = new KeyClass(new string[]{ "1", "2", "3" },
"1");
myDic.Add(kc1, "1");
myDic.Add(kc2, "1");

}
}

class KeyClass : IEquatable<KeyClass>
{
public string[] id;
public string name;

public KeyClass(string[] id, string name) {
this.id = id;
this.name = name;
}

public bool Equals(KeyClass other)
{
Console.WriteLine("Equals called");
if(other.name==name && other.id[0] ==id[0] && other.id[1]
==id[1] && other.id[2] ==id[2])
return true;
else
return false;
}

}

I do not understand why KeyClass.Equals does not get called .

To my understanding the documentation
(http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx) states that
"If TKey implements the system.IEquatable generic interface, the
default equality comparer uses that implementation".

Any alternative idears how to make the example work (getting an
exception trying to add the same key twice)?

 
Reply With Quote
 
 
 
 
Architect
Guest
Posts: n/a
 
      2nd Jan 2007
Hi Kim,

Change it to:

bool IEquatable<KeyClass>.Equals(KeyClass other)
{
Console.WriteLine("Equals called");
if(other.name==name && other.id[0] ==id[0] &&
other.id[1]==id[1] && other.id[2] ==id[2])
return true;
else
return false;
}

Regards,
Valentin Ivanov.


(E-Mail Removed) wrote:
> Hi
>
> I want to use the Dictionary Classs that will load my own class
> called KeyClass used as TKey.
>
> Here is the code:
>
> public class Dictionary
> {
> public static void Main()
> {
> Dictionary<KeyClass, string> myDic = new
> Dictionary<KeyClass, string>();
>
> KeyClass kc1 = new KeyClass(new string[]{ "1", "2", "3" },
> "1");
> KeyClass kc2 = new KeyClass(new string[]{ "1", "2", "3" },
> "1");
> myDic.Add(kc1, "1");
> myDic.Add(kc2, "1");
>
> }
> }
>
> class KeyClass : IEquatable<KeyClass>
> {
> public string[] id;
> public string name;
>
> public KeyClass(string[] id, string name) {
> this.id = id;
> this.name = name;
> }
>
> public bool Equals(KeyClass other)
> {
> Console.WriteLine("Equals called");
> if(other.name==name && other.id[0] ==id[0] && other.id[1]
> ==id[1] && other.id[2] ==id[2])
> return true;
> else
> return false;
> }
>
> }
>
> I do not understand why KeyClass.Equals does not get called .
>
> To my understanding the documentation
> (http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx) states that
> "If TKey implements the system.IEquatable generic interface, the
> default equality comparer uses that implementation".
>
> Any alternative idears how to make the example work (getting an
> exception trying to add the same key twice)?


 
Reply With Quote
 
David Browne
Guest
Posts: n/a
 
      2nd Jan 2007


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi
>
> I want to use the Dictionary Classs that will load my own class
> called KeyClass used as TKey.
>
> Here is the code:
>
> public class Dictionary
> {
> public static void Main()
> {
> Dictionary<KeyClass, string> myDic = new
> Dictionary<KeyClass, string>();
>
> KeyClass kc1 = new KeyClass(new string[]{ "1", "2", "3" },
> "1");
> KeyClass kc2 = new KeyClass(new string[]{ "1", "2", "3" },
> "1");
> myDic.Add(kc1, "1");
> myDic.Add(kc2, "1");
>
> }
> }
>
> class KeyClass : IEquatable<KeyClass>
> {
> public string[] id;
> public string name;
>
> public KeyClass(string[] id, string name) {
> this.id = id;
> this.name = name;
> }
>
> public bool Equals(KeyClass other)
> {
> Console.WriteLine("Equals called");
> if(other.name==name && other.id[0] ==id[0] && other.id[1]
> ==id[1] && other.id[2] ==id[2])
> return true;
> else
> return false;
> }
>
> }
>
> I do not understand why KeyClass.Equals does not get called .
>
> To my understanding the documentation
> (http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx) states that
> "If TKey implements the system.IEquatable generic interface, the
> default equality comparer uses that implementation".
>


IEquatable is a supplement to, not a replacement of, the normal method of
controlling object equality comparison. You should still override
GetHashCode() and Equals(object).


Your particular problem is a result of not overloading GetHashCode(). The
Dictionary first gets the hash code for the two keys, and since the hash
codes are different, it never has to run the Equals method for comparison.
You should also overload the non-generic Equals(object) method so the
comparison behaves the same whether you use a KeyObject reference or an
Object reference.

EG

sealed class KeyClass : IEquatable<KeyClass>
{
///make this an immutable class since we're
///relying on the field values to generate a hash code
public readonly string[] id;
public readonly string name;

public KeyClass(string[] id, string name)
{
this.id = id;
this.name = name;
}

public override int GetHashCode()
{
/// use the hash codes from the fields
/// to generate a reasonable hash for this object.
return name.GetHashCode() ^ id[0].GetHashCode();
}
public override bool Equals(object obj)
{
///just test the type and call IEquatable<T>.Equals
KeyClass other = obj as KeyClass;
if (other == null)
return false;

return Equals(other);
}

public bool Equals(KeyClass other)
{
System.Diagnostics.Trace.WriteLine("Equals called");
if (other.name == name && other.id[0] == id[0] && other.id[1] == id[1]
&& other.id[2] == id[2])
return true;
else
return false;
}

}

David

 
Reply With Quote
 
Architect
Guest
Posts: n/a
 
      2nd Jan 2007
Hello Kim,

Please disregard my previous e-mail.
The reason why your Equals method is not called is because internally
default comparer checks hash code first and then it calls Equals
method.

In your case you have 2 completely different instances. Each will have
its own different hash code. So the first check of hash codes will
return false which will be enough for dictionary to create new entry.

Try overload GetHashCode() method of your KeyClass and you'll see what
I mean

public override int GetHashCode()
{
return 0; //this will return same hash code for all
instances of the KeyClass
}

Regards,
Valentin Ivanov.


Architect wrote:
> Hi Kim,
>
> Change it to:
>
> bool IEquatable<KeyClass>.Equals(KeyClass other)
> {
> Console.WriteLine("Equals called");
> if(other.name==name && other.id[0] ==id[0] &&
> other.id[1]==id[1] && other.id[2] ==id[2])
> return true;
> else
> return false;
> }
>
> Regards,
> Valentin Ivanov.
>
>
> (E-Mail Removed) wrote:
> > Hi
> >
> > I want to use the Dictionary Classs that will load my own class
> > called KeyClass used as TKey.
> >
> > Here is the code:
> >
> > public class Dictionary
> > {
> > public static void Main()
> > {
> > Dictionary<KeyClass, string> myDic = new
> > Dictionary<KeyClass, string>();
> >
> > KeyClass kc1 = new KeyClass(new string[]{ "1", "2", "3" },
> > "1");
> > KeyClass kc2 = new KeyClass(new string[]{ "1", "2", "3" },
> > "1");
> > myDic.Add(kc1, "1");
> > myDic.Add(kc2, "1");
> >
> > }
> > }
> >
> > class KeyClass : IEquatable<KeyClass>
> > {
> > public string[] id;
> > public string name;
> >
> > public KeyClass(string[] id, string name) {
> > this.id = id;
> > this.name = name;
> > }
> >
> > public bool Equals(KeyClass other)
> > {
> > Console.WriteLine("Equals called");
> > if(other.name==name && other.id[0] ==id[0] && other.id[1]
> > ==id[1] && other.id[2] ==id[2])
> > return true;
> > else
> > return false;
> > }
> >
> > }
> >
> > I do not understand why KeyClass.Equals does not get called .
> >
> > To my understanding the documentation
> > (http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx) states that
> > "If TKey implements the system.IEquatable generic interface, the
> > default equality comparer uses that implementation".
> >
> > Any alternative idears how to make the example work (getting an
> > exception trying to add the same key twice)?


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Different generic type parameter in generic class constructorparameter tadmill@yahoo.com Microsoft C# .NET 7 26th May 2008 09:43 AM
Emitting a Generic Type that has a Generic Base Class =?Utf-8?B?UGF0cmljaw==?= Microsoft Dot NET Framework 1 7th Jun 2006 05:14 PM
error C2955: 'Vector' : use of class generic requires generic argument list Herby Microsoft VC .NET 5 14th Mar 2006 03:55 PM
Generic Dictionary not serializing with the rest of a class... =?Utf-8?B?R2FiZSBDb3ZlcnQ=?= Microsoft C# .NET 1 12th Jan 2006 11:14 PM
Generic class derived from NameObjectCollectionBase and generic constraints Gabriel Lozano-Morán Microsoft Dot NET Framework 1 10th Dec 2005 11:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:08 AM.