Mateusz Sowiński
Mateusz Sowiński

Reputation: 439

C# overload operators for primitive types

Is it possible to overload operators such as ||, && and ! for types like int and float?

Example:

float a, b; int c;
//instead of this:
return Convert.ToBoolean(a) || Convert.ToBoolean(b) && !Convert.ToBoolean(c);
//do this:
return a || b && !c;

Upvotes: 1

Views: 358

Answers (3)

Gaurav Mall
Gaurav Mall

Reputation: 2412

For OR you have the | symbol. For example:

return a | b; 

Would return true if either a OR b was true. Otherwise, it would return false.

Also, you have the & operator with which you can do this:

return a & b;

This would return true if both a AND b where true. Otherwise, it would return false. I recommend you to look into to the documentation here for more operators:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators

And of course, as the title of the website says(boolean operators), not it is not possible to do this for integers and floats because they don't have any meaning. I mean why would you want to return 2 or 4 & 5? It just doesn't have any value. Look here if you find anything:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/overloadable-operators

Edit: It actually depends on what you are doing, as you pointed out it can be useful for certain problems/algorithms like dealing with matrices, etc. But because you didn't necessarily specify what kind of thing you were doing I understood it as for everyday programming. Anyways, above are the operators for integers. I don't think you can overload operators with floats, though.

Also, @SohaibJundi said (great work), instead of ! you use this:

return ~a;

This would return the opposite of the value of a. Meaning if a was true it would return false and if it was false it would return true.

Hope it helps!

Upvotes: 1

Sohaib Jundi
Sohaib Jundi

Reputation: 1664

You can use the bit-wise operators to achieve this when using integers.

You would use & instead of &&, | instead of || and ~ instead of !.

It would look like this:

    int a, b, c;
    //instead of this:
    //return Convert.ToBoolean(a) || Convert.ToBoolean(b) && !Convert.ToBoolean(c);
    //do this:
    return (a | b & ~c) != 0;

But in case of floats, you cannot use these operators.

Edit: After giving it more thought, I believe you can't do this. All non zero values get evaluated to true, except zero evaluates to false. Keeping this in mind, and-ing two non zero values using the bit-wise and (&) might evaluate to zero. E.g.: a = 1, b = 2 -> a & b = 0. Also the bit-wise negation (~) would only evaluate to zero if applied on -1. ~1 would evaluate to non zero, when converted to boolean evaluates to true.

Upvotes: 2

m r
m r

Reputation: 21

  1. from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/introduction#expressions

    Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type. Overloading Primitive Operator (C#)

  2. from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators#operator-overloadability

    A user-defined type cannot overload the conditional logical operators && and ||. However, if a user-defined type overloads the true and false operators and the & or | operator in a certain way, the && or || operation, respectively, can be evaluated for the operands of that type.

for more information https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#user-defined-conditional-logical-operators

Upvotes: 1

Related Questions