Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have copied the code that the professor gave me, and I got the error "The name 'HashCode does not exist in the current context". I read about it something and I think it should work. I am using VisualStudio 2019. The line is marked down below.

One of the potential fixes that Visual gives me is to Instal package Microsoft.Bcl.HashCode, but as Microsoft documentation says, it should be in System already.

Didn't found anything about this since it is recently added. There are some uses (same as mine), but don't know why mine doesn't work.

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;
        
    }

}

question from:https://stackoverflow.com/questions/65941418/cannot-use-new-hashcode-to-override-gethashcode-in-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
315 views
Welcome To Ask or Share your Answers For Others

1 Answer

If HashCode isn't available because this is not a .NET Core app, then you can use a custom implementation. Example below based 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

}

Or use the hash code combination that Tuple<> uses

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...