C# GetHashCode与Equals在HashTable表查找时的关系
2014-04-26 21:20
405 查看
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using System.Threading; using System.IO; using System.Security.Cryptography; using Common; namespace ConsoleApplication2 { public class Test { private string _id; public string Id { get { return _id; } set { _id = value; } } public Test(string id) { _id = id; } public override int GetHashCode() { Console.WriteLine("GetHashCode()"); return Id.Length; } public override bool Equals(object obj) { Console.WriteLine("Equals()"); return Id == (obj as Test).Id; } } class Program { /// <summary> /// 如果GetHashCode相等则不用Equals了,否则需要Equals /// </summary> /// <param name="args"></param> static void Main(string[] args) { Dictionary<Test, string> dc = new Dictionary<Test, string>(); Test t1 = new Test("a"); Test t2 = new Test("b"); Test t3 = new Test("cc"); dc.Add(t1, ""); Console.WriteLine("----------------"); Console.WriteLine(dc.ContainsKey(t1)); Console.WriteLine("----------------"); Console.WriteLine(dc.ContainsKey(t2)); Console.WriteLine("----------------"); Console.WriteLine(dc.ContainsKey(t3)); } } }
相关文章推荐
- CLR via C#: GetHashCode与Hashtable,Dictionary
- C# GetHashCode、Equals函数和键值对集合的关系
- An article about Equals and GetHashCode functions in C#
- An article about Equals and GetHashCode functions in C#
- 关于 warning CS0659:“***”重写Object.Equals(object o)但不重写Object.GetHashCode()
- 结构类型需要重载GetHashCode和Equals
- NET(C#):GetHashCode不需要太复杂 转
- 提高C#编程水平的50个要点 之九“理解Reference Equals(), static Equals(), instance Equals(), 和运算行符==之间的关系”
- .NET(C#):GetHashCode 的作用
- C# GetHashCode 部分冲突列表 数字字符串版本
- C# GetHashCode 的实现方式
- .NET(C#):GetHashCode 的作用
- C# GetHashCode 的实现方式
- [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例
- Effective C# Item 10: Understand the Pitfalls of GetHashCode()
- Java hashcode 与 equals的关系
- Hashset、Hashmap、Hashtable与hashcode()和equals()的密切关系
- 【极力分享】[C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例
- GetHashCode与Comparer相互调用关系
- C#中根据注册表查找字体与字体文件路径对应关系