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