Supposed we have a class below
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
public
class TestHash { public
int x; int
y; public
TestHash(int
x, int
y) { this.x = x; this.y = y; } public
override int GetHashCode() { Console.WriteLine("判断hashcode"); return
x + y; } public
override bool Equals(object
obj) { Console.WriteLine("判断equals"); return
base.Equals(obj); } public
override string ToString() { return
x.ToString() + y.ToString(); } } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
Hashtable ht = new
Hashtable(); TestHash cc = new
TestHash(2, 3); TestHash cc2 = new
TestHash(1, 4); TestHash cc3 = new
TestHash(3, 3); ht.Add(cc, "test1"); ht.Add(cc2, "test2"); ht.Add(cc3, "test3"); Console.WriteLine("Begin print...."); foreach
(TestHash im in
ht.Keys) { Console.WriteLine(im.ToString() + " ----- "
+ ht[im]); } Console.Read(); |
See also:
http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
http://msdn.microsoft.com/zh-cn/library/system.object.gethashcode(v=vs.110).aspx
understand equal and gethashcode,布布扣,bubuko.com
understand equal and gethashcode
原文:http://www.cnblogs.com/malaikuangren/p/3600876.html