我正确使用dictinalry泛型类吗? [英] am i using the dictinalry generic class correctly?

查看:83
本文介绍了我正确使用dictinalry泛型类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..


i希望建立一个字典类,它将加载我自己的类

被称为字母,

i懂我实现了IEquatable接口的equles方法

然后字典将使用它来比较键,所以

i继续在LETTER类中实现它所以信会

provice它自己的比较手段,但字典忽略它


只有当我创建一个新的类,称为letterkey,它实现了

EqualityComparer将与类比较,让它的GetHashCode

总是返回0,只有我设法比较这两个类。


是吗?这是我开始工作的唯一方式。


现在我有:


Dictionay< Letter,Letter> letters = new Dictionay< Letters,Letters>(new

LetterKey())

hi..

i wanted to build a Dictionary Classs that will load my own class
called letter,
i understood that i implement the IEquatable interface''s equles method
that then the dictionary would use that inorder to compare the keys, so
i went ahead the implented it in the LETTER class so letter would
provice it''s own means for comparison, but the dictionary ignored it

only when i created a new class, called it letterkey ,had it implement
EqualityComparer that will compare to classes , have it''s GetHashCode
always return 0, only i managed to compare the two classes.

is that right? that''s the only way i got it to work.

so now i have:

Dictionay<Letter,Letter> letters= new Dictionay<Letters,Letters>(new
LetterKey())

推荐答案

Elhanan< ; EM ***** @ hotmail.com>写道:
Elhanan <em*****@hotmail.com> wrote:
嗨..

我想构建一个字典类,它将加载我自己的类
被叫信,
我明白我实现了IEquatable接口的'equles方法
然后字典将使用它来比较键,所以
我继续在LETTER类中实现它,所以信会
provice it'自己的比较手段,但字典忽略它
hi..

i wanted to build a Dictionary Classs that will load my own class
called letter,
i understood that i implement the IEquatable interface''s equles method
that then the dictionary would use that inorder to compare the keys, so
i went ahead the implented it in the LETTER class so letter would
provice it''s own means for comparison, but the dictionary ignored it




你能发一个简短但完整的程序来演示

问题吗? br />

请参阅 http:/ /www.pobox.com/~skeet/csharp/complete.html 了解详情

我的意思是什么。


这里'一些示例代码* * *工作:


使用System;

使用System.Collections.Generic;


class Letter:IEquatable< Letter>

{< br $>
char值;


公开信(char值)

{

this.value = value ;

}


公共布尔等于(信其他)

{

Console.WriteLine( Equals called);

返回other.value == value;

}

}


class Test

{

static void Main()

{

Dictionary< Letter,Letter> map = new Dictionary< Letter,Letter>();


Letter c = new Letter(''c'');

map [c] = c;

Console.WriteLine(map [c] == c);

}

}


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件



Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here''s some example code which *does* work:

using System;
using System.Collections.Generic;

class Letter : IEquatable<Letter>
{
char value;

public Letter(char value)
{
this.value = value;
}

public bool Equals(Letter other)
{
Console.WriteLine ("Equals called");
return other.value==value;
}
}

class Test
{
static void Main()
{
Dictionary<Letter,Letter> map= new Dictionary<Letter,Letter>();

Letter c = new Letter(''c'');
map[c]=c;
Console.WriteLine (map[c]==c);
}
}

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


Elhanan,


你需要让IEquatable接口实现返回一个哈希值
代码。如果你没有它返回传入的实例的哈希码,

那么你几乎杀死了哈希表的效率。由于

所有键都将返回相同的哈希码,因此只使用一个桶,

并且您必须遍历所有项目才能找到密钥。


为什么不直接返回传入的实例的哈希值?

-

- Nicholas Paldino [。 NET / C#MVP]

- mv*@spam.guard.caspershouse.com
" Elhanan" < EM ***** @ hotmail.com>在消息中写道

news:11 ********************* @ g44g2000cwa.googlegro ups.com ...
Elhanan,

You need to have the IEquatable interface implementation return a hash
code. If you don''t have it return a hash code for the instances passed in,
then you are pretty much killing the efficiency of the hash table. Since
all of the keys will return the same hashcode, only one bucket will be used,
and you will have to loop through all of the items to find the key.

Why not just return the hash value of the instances that are passed in?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Elhanan" <em*****@hotmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
嗨..

我想构建一个字典类,它将加载我自己的类
被称为字母,
我明白我实现了IEquatable接口'的equles方法
然后字典会使用它来比较键,所以
我继续在LETTER类中实现它,所以信件将提供它自己的比较方法,但字典忽略它

只有当我创建一个新的类,称为letterkey,它实现了EqualityComparer,它将与类进行比较,让它的GetHashCode始终是
返回0,只有我设法比较两个班级。

是吗?这是我开始工作的唯一方式。

现在我有:

Dictionay< Letter,Letter> letters = new Dictionay< Letters,Letters>(new
LetterKey())
hi..

i wanted to build a Dictionary Classs that will load my own class
called letter,
i understood that i implement the IEquatable interface''s equles method
that then the dictionary would use that inorder to compare the keys, so
i went ahead the implented it in the LETTER class so letter would
provice it''s own means for comparison, but the dictionary ignored it

only when i created a new class, called it letterkey ,had it implement
EqualityComparer that will compare to classes , have it''s GetHashCode
always return 0, only i managed to compare the two classes.

is that right? that''s the only way i got it to work.

so now i have:

Dictionay<Letter,Letter> letters= new Dictionay<Letters,Letters>(new
LetterKey())



IEquatable没有GetHashCode方法,iEqualityComparaor,

然而,当我简单地返回实例的哈希值时,当我添加到对象时,我从未得到add方法上的重复键的

异常

具有相同的关键属性,这应该是有意义的,因为每个

对象确实有一个不同的哈希码。

IEquatable does not have a GetHashCode method, iEqualityComparaor does,
however when i simply returned the hash of the instance, i never got an
exception of a duplicatekey on the add method when i added to objects
which had the same key properties, which should make sense becoue each
object did have a different hashcode.


这篇关于我正确使用dictinalry泛型类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆