C# Language Keywords true, false

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The true and false keywords have two uses:

  1. As literal Boolean values
var myTrueBool = true;
var myFalseBool = false;
  1. As operators that can be overloaded
public static bool operator true(MyClass x)
{
    return x.value >= 0;
}

public static bool operator false(MyClass x)
{
    return x.value < 0;
}

Overloading the false operator was useful prior to C# 2.0, before the introduction of Nullable types.
A type that overloads the true operator, must also overload the false operator.



Got any C# Language Question?