| title | true Operator (C# Reference) | |
|---|---|---|
| ms.date | 07/20/2015 | |
| ms.prod | .net | |
| ms.technology |
|
|
| ms.topic | article | |
| helpviewer_keywords |
|
|
| ms.assetid | acaba817-5da5-4364-b3b2-2e5c75ec1839 | |
| caps.latest.revision | 19 | |
| author | BillWagner | |
| ms.author | wiwagn |
Returns the bool value true to indicate that an operand is true and returns false otherwise.
Prior to C# 2.0, the true and false operators were used to create user-defined nullable value types that were compatible with types such as SqlBool. However, the language now provides built-in support for nullable value types, and whenever possible you should use those instead of overloading the true and false operators. For more information, see Nullable Types.
With nullable Booleans, the expression a != b is not necessarily equal to !(a == b) because one or both of the values might be null. You need to overload both the true and false operators separately to correctly identify the null values in the expression. The following example shows how to overload and use the true and false operators.
[!code-csharpcsrefKeywordsOperator#16]
A type that overloads the true and false operators can be used for the controlling expression in if, do, while, and for statements and in conditional expressions.
If a type defines operator true, it must also define operator false.
A type cannot directly overload the conditional logical operators (&& and ||), but an equivalent effect can be achieved by overloading the regular logical operators and operators true and false.
[!INCLUDECSharplangspec]
C# Reference
C# Programming Guide
C# Keywords
C# Operators
false