My code for operator overloading in C# for True and false is given below:
class LimitedInt
{
public static bool operator true(LimitedInt x)
{
if (x.theValue > 10)
return true;
else
return false;
}
public static bool operator false(LimitedInt x)
{
if (x.theValue < 10)
return false;
else
return true;
}
public int theValue;
}
Now I am calling the True and the False methods like:
LimitedInt li1 = new LimitedInt();
li1.theValue = 10;
if (li1)
Console.WriteLine("True Called");
else if (!li1)
Console.WriteLine ("False Called");
For the last Line of the code "else if(!a)" i get the error:
Operator '!' cannot be applied to operand of type 'LimitedInt'
My question is how to call the False method then ?
Update: I am able to call the False overload as suggested by people here. First I have to add the overload of &:
public static LimitedInt operator &(LimitedInt t, LimitedInt s) { return t;}
Then to call false overload do this:
if (li1 && li1)
{
}
Hope this helps others too.