简体   繁体   中英

Overloading >= operator

I need to overload operator >= . If the condition is true, the operator returns true , otherwise false . If at least one of the objects is null – throw an exception ( ArgumentException ). I tried this. What's wrong?

public static bool operator false(Staff inputPerson)
{
    if ((inputPerson.salary) <= 15000)
    {
        return true;
    }
    else if ((inputPerson.salary) is null)
    {
        throw new ArgumentOutOfRangeException("This person does not have a job");
    }

    return false;
}

You need to do something like public static bool operator <= (Rational rational1, Rational rational2)

When you overload this, you need to ensure that you overload all the related operators too. eg <, > <=, >= etc. as well as the equality operators and methods.

You need to pass in both of the objects to be compared, as the method is static, and not an instance method.

Try this:

public static bool operator >=(Staff p1, Staff p2) {
    if (p1 is null || p2 is null) {
        throw new ArgumentOutOfRangeException("This person does not have a job");
    }
    return p1.salary >= p2.salary;
}

Source: http://msdn.microsoft.com/en-us/library/aa288467%28v=vs.71%29.aspx

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