现在的位置: 首页 > 综合 > 正文

GetHashCode(一)为啥 如何重写

2013年05月20日 ⁄ 综合 ⁄ 共 3069字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HashCode
{
    class Program
    {
        static void Main(string[] args)
        {
             //GetHashCode 值
            A a1 = new A(1);//1
            A a2 = new A(2);//0
            A a3 = new A(3);//1

            Dictionary<A, object> dic = new Dictionary<A, object>();
            dic.Add(a1, 123);

            Console.WriteLine("分隔符");
            Console.WriteLine(dic.ContainsKey(a2));

            Console.WriteLine("分隔符");
            Console.WriteLine(dic.ContainsKey(a3));

            Console.WriteLine();

        }
    }


    class A
    {
        public int Id { get; set; }

        public A(int id)
        {
            this.Id = id;
        }

        public override bool Equals(object obj)
        {
            Console.WriteLine("Equal 被执行");

            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            return Id == (obj as A).Id;
        }

        public override int GetHashCode()
        {
            Console.WriteLine("GetHashCode 被执行");

            return Id % 2;
        }
    }
}

 

GetHashCode()方法的实现必须遵循如下三条规则:
1,如果两个对象相等,它们必须产生相同的散列码
2,对于任意对象o,o.GetHashCode()必须是一个实例不变式,也就是说无论在o上调用什么方法,o.GetHashCode()必须返回同样的值。
3,散列函数应该在所有整数中产生一个随机的分布,这样才能获得效率的提升

下面我说说GetHashCode()方法的默认实现:

对于引用类型来说,他们的GetHashCode实现是直接继承了Object.GetHashCode()的实 现,Object.GetHashCode()使用Object的一个内部字段(对象标识字段)来产生散列值。从1开始,每创建一个新对象,这个字段也随 之增长,这个字段在构造器中设置,设置之后就不能更改了,对一个给定对象,就使用这个字段的值作为散列值。
对照上面的规则:
对于规则1,默认情况下,两个对象相等意味着引用同一个对象,既然是同一个对象,对象标识字段也一样,所以这个默认的实现可以满足规则1。
对于规则2,对象标识字段设置后就不能改变了,所以这个默认的实现可以满足规则2。
对于规则3,除非你创建了大量的对象,不然生成的散列码不可能随机分布

对于值类型来说,值类型都是继承于ValueType的,而ValueType重写了GetHashCode()方法,默认的实现会返回类型中第一个字段的散列值作为对象的散列值。
对照上面的规则:
对于规则1,默认情况下,如果两个值类型实例相等,那么它们的每一个字段都相等,当然第一个字段也相等,第一个字段的散列值也就相等,默认的实现是返回类型中第一个字段的散列值作为对象的散列值,所以这个默认的实现可以满足规则1
对于规则2,除非第一个字段是常量,否则默认的实现不能满足规则2
对于规则3,主要看第一个字段的GetHashCode()方法是否满足规则3

总结:
在默认情况下,引用类型的GetHashCode()方法的实现可以正确的工作,但是效率低下;
在默认情况下,值类型的GetHashCode()方法的实现常常是不正确的。

 

BusLine bl_1 = new BusLine() { Name = "1路", Stations = new List<string>() { "1", "2", "3" } };
            BusLine bl_2 = new BusLine() { Name = "1路", Stations = new List<string>() { "1", "2", "3" } };
            BusLine bl_3 = new BusLine() { Name = "2路", Stations = new List<string>() { "1", "2", "3" } };
            //Queue<BusLine> que = new Queue<BusLine>();
            //que.Enqueue(bl_1);
            HashSet<BusLine> que = new HashSet<BusLine>();
            Console.WriteLine("-------");
            que.Add(bl_1);
            Console.WriteLine("------");
            if (que.Contains(bl_2))
            {
                Console.WriteLine("包含");
            }
            else
            {
                Console.WriteLine("不包含");
            }
            Console.WriteLine("------");
            if (que.Contains(bl_3))
            {
                Console.WriteLine("包含");
            }
            else
            {
                Console.WriteLine("不包含");
            }
            Console.ReadLine();

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqJoin
{
    class Program
    {
        static void Main(string[] args)
        {
            HashTest();
        }

        private static void HashTest()
        {
            //List<Person> list = new List<Person>();
            HashSet<Person> list = new HashSet<Person>();
            Person p_1 = new Person("A", 18);
            Person p_2 = new Person("B", 12);
            Person p_3 = new Person("B", 12);
            list.Add(p_1);
            Console.WriteLine("----------------");
            list.Add(p_2);
            Console.WriteLine("----------------");
            list.Add(p_3);
            Console.WriteLine("--------------");

            var query = list.Distinct().ToList();
            Console.WriteLine();
        }
    }
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }

        public override bool Equals(object obj)
        {
            Console.WriteLine("equals run");
            Person p = obj as Person;
            return this.Name == p.Name && this.Age == p.Age;
        }

        public override int GetHashCode()
        {
            Console.WriteLine("hash run");
            return Name.GetHashCode() ^ Age.GetHashCode();
        }
    }
}

抱歉!评论已关闭.