|
1 | 1 | --- |
2 | | -title: "false Operator (C# Reference)" |
| 2 | +title: "false operator (C# Reference)" |
3 | 3 | ms.date: 07/20/2015 |
4 | 4 | helpviewer_keywords: |
5 | 5 | - "false operator keyword [C#]" |
6 | 6 | ms.assetid: 81a888fd-011e-4589-b242-6c261fea505e |
7 | 7 | --- |
8 | | -# false Operator (C# Reference) |
9 | | -Returns the [bool](../../../csharp/language-reference/keywords/bool.md) value `true` to indicate that an operand is `false` and returns `false` otherwise. |
10 | | - |
11 | | - 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](../../../csharp/programming-guide/nullable-types/index.md). |
12 | | - |
13 | | - 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 have to overload both the `true` and `false` operators separately to correctly handle the null values in the expression. The following example shows how to overload and use the `true` and `false` operators. |
14 | | - |
15 | | - [!code-csharp[csrefKeywordsOperator#16](../../../csharp/language-reference/keywords/codesnippet/CSharp/false-operator_1.cs)] |
16 | | - |
17 | | - A type that overloads the `true` and `false` operators can be used for the controlling expression in [if](../../../csharp/language-reference/keywords/if-else.md), [do](../../../csharp/language-reference/keywords/do.md), [while](../../../csharp/language-reference/keywords/while.md), and [for](../../../csharp/language-reference/keywords/for.md) statements and in [conditional expressions](../../../csharp/language-reference/operators/conditional-operator.md). |
18 | | - |
19 | | - If a type defines operator `false`, it must also define operator [true](../../../csharp/language-reference/keywords/true.md). |
20 | | - |
21 | | - A type cannot directly overload the conditional logical operators [&&](../../../csharp/language-reference/operators/conditional-and-operator.md) and [||](../../../csharp/language-reference/operators/conditional-or-operator.md), but an equivalent effect can be achieved by overloading the regular logical operators and operators `true` and `false`. |
22 | | - |
23 | | -## C# Language Specification |
24 | | - [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] |
25 | | - |
26 | | -## See Also |
27 | | - [C# Reference](../../../csharp/language-reference/index.md) |
28 | | - [C# Programming Guide](../../../csharp/programming-guide/index.md) |
29 | | - [C# Keywords](../../../csharp/language-reference/keywords/index.md) |
30 | | - [C# Operators](../../../csharp/language-reference/operators/index.md) |
31 | | - [true](../../../csharp/language-reference/keywords/true.md) |
| 8 | +# false operator (C# Reference) |
| 9 | + |
| 10 | +Returns the [bool](bool.md) value `true` to indicate that an operand is `false` and returns `false` otherwise. |
| 11 | + |
| 12 | +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](../../programming-guide/nullable-types/index.md). |
| 13 | + |
| 14 | +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 have to overload both the `true` and `false` operators separately to correctly handle the null values in the expression. The following example shows how to overload and use the `true` and `false` operators. |
| 15 | + |
| 16 | +[!code-csharp[csrefKeywordsOperator#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsOperator/CS/csrefKeywordsOperators.cs#16)] |
| 17 | + |
| 18 | +A type that overloads the `true` and `false` operators can be used for the controlling expression in [if](if-else.md), [do](do.md), [while](while.md), and [for](for.md) statements and in [conditional expressions](../operators/conditional-operator.md). |
| 19 | + |
| 20 | +If a type defines operator `false`, it must also define operator [true](true.md). |
| 21 | + |
| 22 | +A type cannot directly overload the conditional logical operators [&&](../operators/conditional-and-operator.md) and [||](../operators/conditional-or-operator.md), but an equivalent effect can be achieved by overloading the regular logical operators and operators `true` and `false`. |
| 23 | + |
| 24 | +## C# language specification |
| 25 | + |
| 26 | +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] |
| 27 | + |
| 28 | +## See also |
| 29 | + |
| 30 | +[C# Reference](../index.md) |
| 31 | +[C# Programming Guide](../../programming-guide/index.md) |
| 32 | +[C# Keywords](index.md) |
| 33 | +[C# Operators](../operators/index.md) |
| 34 | +[true](true.md) |
0 commit comments