C#开发之——GetHashCode方法(6.4)

一 概述

  • C# GetHashCode方法返回当前System.Object的哈希代码,每个对象的哈希值都是固定的

  • 该方法不含有任何参数,并且 不是静态方法,因此需要使用实例来调用该方法

  • 由于该方法是在Object类中定义的,因此任何对象都可以直接调用该方法

<!--more-->

二 实例 创建两个Student类的对象,并分别计算其哈希值

2.1 代码

<span style="color:#333333">class Program
{
    static void Main(string[] args)
    {
        Student stu1 = new Student();
        Student stu2 = new Student();
        Console.WriteLine(stu1.GetHashCode());
        Console.WriteLine(stu2.GetHashCode());
    }
}</span>

2.2 说明

  • 从执行结果可以看出,不同实例的哈希值是不同的,因此也可以通过该方法比较对象是否相等

猜你喜欢

转载自blog.csdn.net/Calvin_zhou/article/details/107498530
今日推荐