简体   繁体   中英

How can I overload the “==” operator to determine of two ID numbers are the same?

I need to write a method that will take two employee objects as input parameters and compare their ID's to determine whether or not they match.

It's incomplete at the moment; but so far, I have an Employee class that inherits first and last name properties from a "Person" class, and has ID as its own property. I'm writing the method in the employee file and have already instantiated 2 example employees in my program. As for overloading the ==, I'm running into an error that says "Employee" defines operator == but does not override Object.Equals." It also says I need to define "!=", but I'm confused on how to set up the != overload when it doesn't even figure into this method.

I've seen two ways of doing a compare method, one that returns true or false, and another that simply writes "match" to the console. Either of those would work for my purposes, but I can't figure out a workaround for the errors or how I'd change the code in this situation in order to determine a match between 2 employee ID's. Here is my code below; I'd appreciate any input on what may be going wrong! (I have a feeling it may be very off). I'm also unsure of how to call the method but I'm currently trying to figure it out.

Program file:

namespace OperatorOverload
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee example = new Employee();

            example.FirstName = "Kitty";
            example.LastName = "Katz";
            example.ID = 24923;

            Employee example2 = new Employee();
            example2.FirstName = "John";
            example2.LastName = "Dudinsky";
            example2.ID = 39292;

            Console.ReadLine();
        }
    }
}

Employee Class:

namespace OperatorOverload
{
    class Employee : Person
    {
        public int ID { get; set; }

        public static bool operator==(Employee employee, Employee employee2)
        {
            if (employee.ID == employee2.ID)

                return true;        
            else           
                return false;
            }
        }
    }

Person Class:

namespace OperatorOverload

{
    public class Person
    {
       public string FirstName { get; set; }
        public string LastName { get; set; }      
    }
}

you need to also override the Equals method:

public override bool Equals(Object Obj)
{
    Person person = obj as Person;

    if(person == null)
        return false;

    return this.ID.Equals(person.ID);
}

Microsoft's recommandations :

Implement the GetHashCode method whenever you implement the Equals method. This keeps Equals and GetHashCode synchronized.

Override the Equals method whenever you implement the equality operator (==), and make them do the same thing.

What You need to do is to use the Equals function and override it like this:

public override bool Equals(object obj)
{
    var item = obj as Employee;

    if (item == null)
    {
        return false;
    }

    return this.ID.Equals(item.ID);
}

The compiler is basically telling you that if you want to overload the == operator for the class Employee you will also have to override Object.Equals method and the != operator so that you will get a consistent semantic which you can use to compare instances of type Employee.

This means that you must not look for workarounds: you just have to overload the != operator and override Object.Equals so that Employee objects are compared by ID and not by reference (as they do by default if you don't provide your own equality semantic).

You should use this class. Override operator!=, operator== and Equals(object) method.

class Employee : Person
    {
        public int ID { get; set; }

        public static bool operator ==(Employee employee, Employee employee2)
        {
            if (employee.ID == employee2.ID)
                return true;
            else
                return false;

            //but you should use 
            //return employee.ID == employee2.ID;
        }

        public static bool operator !=(Employee employee, Employee employee2)
        {
            return employee.ID != employee2.ID;
        }

        public override bool Equals(object obj)
        {
            var emp = obj as Employee;
            if (emp == null)
                return false;

            return this.ID.Equals(emp.ID);
        }
    }

This is a better way to override a Equals method:

public override bool Equals(object obj)
{
    if (obj is null) return false;
    if (ReferenceEquals(this, obj)) return true;
    if (obj.GetType() != this.GetType()) return false; //optional. depends on logic
    return this.ID.Equals(((Employee)obj).ID);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM