程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了不能使用新的 HashCode 覆盖类中的 GetHashCode大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决不能使用新的 HashCode 覆盖类中的 GetHashCode?

开发过程中遇到不能使用新的 HashCode 覆盖类中的 GetHashCode的问题如何解决?下面主要结合日常开发的经验,给出你关于不能使用新的 HashCode 覆盖类中的 GetHashCode的解决方法建议,希望对你解决不能使用新的 HashCode 覆盖类中的 GetHashCode有所启发或帮助;

我已经复制了教授给我的代码,但出现错误“名称'HashCode 在当前上下文中不存在”。我读了一些关于它的东西,我认为它应该有效。我使用的是 Visualstudio 2019。该行在下方标记。

Visual 给我的潜在修复之一是安装包 Microsoft.bcl.HashCode,但正如 Microsoft 文档所说,它应该已经在系统中。

没有发现任何关于此的信息,因为它是最近添加的。有一些用途(和我的一样),但不知道为什么我的不起作用。

using System;
using System.Collections.Generic;
using System.linq;
using System.Text;
using System.Threading.Tasks;

namespace LV6.Zad3 {
    public class Car : IEquatable<Car>
        {
        public String Make { get; private set; }
        public String Model { get; private set; }
        public int Km { get; private set; }
        public int Year { get; private set; }
        
        public Car(String brand,String type,int km,int year)
        {
            Make = brand;
            Model = type;
            Km = km;
            Year = year;
        }
        
        public overrIDe String ToString() => $"{MakE} - {Model} - {Km} - {Year}";
        
        public overrIDE int GetHashCode() => HashCode.Combine(Make,Model,Km,Year);  // I this line <--
        
        public overrIDe bool Equals(object obj){
            if (obj is Car == falsE) return false;
            return this.Equals((Car) obj);
        }
        
        public bool Equals(Car other) => this.Make == other.Make &&
            this.Model == other.Model &&
            this.Year == other.Year &&
            this.Km == other.Km;
        
    }

}

解决方法

如果 HashCode 不可用,因为这不是 .NET Core 应用,那么您可以使用自定义实现。下面的示例基于 on this accepted answer。

public class Car : IEquatable<Car>
{
    public String Make { get; private set; }
    public String Model { get; private set; }
    public int Km { get; private set; }
    public int Year { get; private set; }

    public Car(String brand,String type,int km,int year)
    {
        Make = brand;
        Model = type;
        Km = km;
        Year = year;
    }

    public override String ToString() => $"{MakE} - {Model} - {Km} - {Year}";


    #region IEquatable Members
    /// <sumMary>
    /// Equality overrides from <see cref="System.Object"/>
    /// </sumMary>
    /// <param name="obj">The object to compare this with</param>
    /// <returns>false if object is a different type,otherwise it calls <code>Equals(Car)</code></returns>
    public override bool Equals(object obj)
    {
        if (obj is Car other)
        {
            return Equals(other);
        }
        return false;
    }

    /// <sumMary>
    /// checks for equality among <see cref="Car"/> classes
    /// </sumMary>
    /// <param name="other">The other <see cref="Car"/> to compare it to</param>
    /// <returns>True if equal</returns>
    public virtual bool Equals(Car other)        
        => this.Make == other.Make &&
        this.Model == other.Model &&
        this.Year == other.Year &&
        this.Km == other.Km;
    

    /// <sumMary>
    /// Calculates the hash code for the <see cref="Car"/>
    /// </sumMary>
    /// <returns>ThE int hash value</returns>
    public overridE int GetHashCode()
    {
        unchecked
        {
            int hc = -1817952719;
            hc = (-1521134295)*hc + Make.GetHashCode();
            hc = (-1521134295)*hc + Model.GetHashCode();
            hc = (-1521134295)*hc + Year.GetHashCode();
            hc = (-1521134295)*hc + Km.GetHashCode();
            return hc;
        }
    }

    #endregion

}

或者使用Tuple<>使用的哈希码组合

// System.Tuple
internal static int CombineHashCodes(int h1,int h2)
{
    return ((h1 << 5) + h1) ^ h2;
}

大佬总结

以上是大佬教程为你收集整理的不能使用新的 HashCode 覆盖类中的 GetHashCode全部内容,希望文章能够帮你解决不能使用新的 HashCode 覆盖类中的 GetHashCode所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: