What is the purpose and effect of the true
and false
operators in C#? The official documentation on these is hopelessly non-explanatory.
The true and false operators can be overloaded, to allow a class to represent its own state as true or false, for example:
public class MyClass
{
//...
public static bool operator true(MyClass op)
{
// Evaluation code...
}
public static bool operator false(MyClass op)
{
// Evaluation code...
}
}
And you will be able to use the operator in boolean expressions:
MyClass test = new MyClass(4, 3);
if (test)
Console.WriteLine("Something true");
else
Console.WriteLine("Something false");
string text = test ? "Returned true" : "Returned false";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With