本文介绍了我正确使用dictinalry泛型类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨.. 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 classcalled letter,i understood that i implement the IEquatable interface''s equles methodthat then the dictionary would use that inorder to compare the keys, soi went ahead the implented it in the LETTER class so letter wouldprovice it''s own means for comparison, but the dictionary ignored itonly when i created a new class, called it letterkey ,had it implementEqualityComparer that will compare to classes , have it''s GetHashCodealways 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>(newLetterKey())推荐答案 你能发一个简短但完整的程序来演示 问题吗? br /> 请参阅 http:/ /www.pobox.com/~skeet/csharp/complete.html 了解详情 我的意思是什么。 这里'一些示例代码* * *工作: 使用System; 使用System.Collections.Generic; class Letter:IEquatable< Letter> { 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 theproblem?See http://www.pobox.com/~skeet/csharp/complete.html for details ofwhat 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.skeetIf replying to the group, please do not mail me too 这篇关于我正确使用dictinalry泛型类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 15:54