<p>There are two weird operators in C#:</p>

<ul>
<li>the <a href="http://msdn.microsoft.com/en-us/library/6x6y6z4d.aspx" rel="noreferrer">true operator</a></li>
<li>the <a href="http://msdn.microsoft.com/en-us/library/6292hy1k.aspx" rel="noreferrer">false operator</a></li>
</ul>

<p>If I understand this right these operators can be used in types which I want to use instead of a boolean expression and where I don't want to provide an implicit conversion to bool.</p>

<p>Let's say I have a following class:</p>

<pre><code>    public class MyType
    {
        public readonly int Value;

        public MyType(int value)
        {
            Value = value;
        }

        public static bool operator true (MyType mt)
        {
            return  mt.Value &gt; 0;
        }

        public static bool operator false (MyType mt)
        {
            return  mt.Value &lt; 0;
        }

    }
</code></pre>

<p>So I can write the following code:</p>

<pre><code>    MyType mTrue = new MyType(100);
    MyType mFalse = new MyType(-100);
    MyType mDontKnow = new MyType(0);

    if (mTrue)
    {
         // Do something.
    }

    while (mFalse)
    {
        // Do something else.
    }

    do
    {
        // Another code comes here.
    } while (mDontKnow)
</code></pre>

<p>However for all the examples above only the true operator is executed. So what's the false operator in C# good for?</p>

<p><em>Note: More examples can be found <a href="http://www.java2s.com/Tutorial/CSharp/0160__Operator-Overload/truefalseoperatorforComplex.htm" rel="noreferrer">here</a>, <a href="http://www.java2s.com/Tutorial/CSharp/0160__Operator-Overload/OverloadtrueandfalseforTwoDimension.htm" rel="noreferrer">here</a> and <a href="http://www.java2s.com/Tutorial/CSharp/0160__Operator-Overload/trueandfalseoperator.htm" rel="noreferrer">here</a>.</em></p>

[{'answer_id': 33315, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 4, 'selected': False, 'text': '<p>The page you link to  <a href="http://msdn.microsoft.com/en-us/library/6x6y6z4d.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/6x6y6z4d.aspx</a> says what they were for, which was a way of handling nullable bools before nullable value types were introduced.</p>\n\n<p>I\'d guess nowadays they\'re good for the same sort of stuff as ArrayList - i.e. absolutely nothing.</p>\n'}, {'answer_id': 33337, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 2, 'selected': False, 'text': "<p>It appears from the MSDN article you linked to it was provided to allow for nullable boolean types prior to the Nullable (i.e. int?, bool?, etc.) type being introducted into the language in C#2. Thus you would store an internal value indicating whether the value is true or false or null, i.e. in your example >0 for true, &lt;0 for false and ==0 for null, and then you'd get SQL-style null semantics. You would also have to implement a .IsNull method or property in order that nullity could be checked explicitly. </p>\n\n<p>Comparing to SQL, imagine a table Table with 3 rows with value Foo set to true, 3 rows with value Foo set to false and 3 rows with value Foo set to null.</p>\n\n<pre><code>SELECT COUNT(*) FROM Table WHERE Foo = TRUE OR Foo = FALSE\n6\n</code></pre>\n\n<p>In order to count all rows you'd have to do the following:-</p>\n\n<pre><code>SELECT COUNT(*) FROM Table WHERE Foo = TRUE OR Foo = FALSE OR Foo IS NULL\n9\n</code></pre>\n\n<p>This 'IS NULL' syntax would have equivilent code in your class as .IsNull().</p>\n\n<p>LINQ makes the comparison to C# even clearer:-</p>\n\n<pre><code>int totalCount = (from s in MyTypeEnumerable\n                 where s || !s\n                 select s).Count();\n</code></pre>\n\n<p>Imagining that MyTypeEnumberable has exactly the same contents of the database, i.e. 3 values equal to true, 3 values equal to false and 3 values equal to null. In this case totalCount would evaluate to 6 in this case. However, if we re-wrote the code as:-</p>\n\n<pre><code>int totalCount = (from s in MyTypeEnumerable\n                 where s || !s || s.IsNull()\n                 select s).Count();\n</code></pre>\n\n<p>Then totalCount would evaluate to 9.</p>\n\n<p>The DBNull example given in the linked MSDN article on the false operator demonstrates a class in the BCL which has this exact behaviour.</p>\n\n<p>In effect the conclusion is you shouldn't use this unless you're completely sure you want this type of behaviour, it's better to just use the far simpler nullable syntax!!</p>\n\n<p><strong>Update:</strong> I just noticed you need to manually override the logic operators !, || and &amp;&amp; to make this work properly. I believe the false operator feeds into these logical operators, i.e. indicating truth, falsity or 'otherwise'. As noted in another comment !x won't work off the bat; you have to overload !. Weirdness!</p>\n"}, {'answer_id': 33339, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 3, 'selected': False, 'text': "<p>AFAIK, it would be used in a test for false, such as when the <code>&amp;&amp;</code> operator comes into play. Remember, &amp;&amp; short-circuits, so in the expression</p>\n\n<pre><code>if ( mFalse &amp;&amp; mTrue) \n{\n   // ... something\n}\n</code></pre>\n\n<p><code>mFalse.false()</code> is called, and upon returning <code>true</code> the expression is reduced to a call to 'mFalse.true()' (which should then return <code>false</code>, or things will get weird).</p>\n\n<p>Note that you must implement the <code>&amp;</code> operator in order for that expression to compile, since it's used if <code>mFalse.false()</code> returns <code>false</code>.</p>\n"}, {'answer_id': 33391, 'author': 'Nir', 'author_id': 3509, 'author_profile': 'https://Stackoverflow.com/users/3509', 'pm_score': 7, 'selected': True, 'text': '<p>You can use it to override the <code>&amp;&amp;</code> and <code>||</code> operators.</p>\n\n<p>The <code>&amp;&amp;</code> and <code>||</code> operators can\'t be overridden, but if you override <code>|</code>, <code>&amp;</code>, <code>true</code> and <code>false</code> in exactly the right way the compiler will call <code>|</code> and <code>&amp;</code> when you write <code>||</code> and <code>&amp;&amp;</code>.</p>\n\n<p>For example, look at this code (from <a href="http://ayende.com/blog/1574/nhibernate-criteria-api-operator-overloading" rel="noreferrer">http://ayende.com/blog/1574/nhibernate-criteria-api-operator-overloading</a> - where I found out about this trick; <a href="http://web.archive.org/web/20080613013350/http://www.ayende.com/Blog/archive/2006/08/04/7381.aspx" rel="noreferrer">archived version</a> by @BiggsTRC):</p>\n\n<pre><code>public static AbstractCriterion operator &amp;(AbstractCriterion lhs, AbstractCriterion rhs)\n{\n       return new AndExpression(lhs, rhs);\n}\n\npublic static AbstractCriterion operator |(AbstractCriterion lhs, AbstractCriterion rhs)\n{\n       return new OrExpression(lhs, rhs);\n}\n\npublic static bool operator false(AbstractCriterion criteria)\n{\n       return false;\n}\npublic static bool operator true(AbstractCriterion criteria)\n{\n       return false;\n}\n</code></pre>\n\n<p>This is obviously a side effect and not the way it\'s intended to be used, but it is useful.</p>\n'}, {'answer_id': 33437, 'author': 'Jakub Šturc', 'author_id': 2361, 'author_profile': 'https://Stackoverflow.com/users/2361', 'pm_score': 5, 'selected': False, 'text': '<p>Shog9 and Nir:\nthanks for your answers. Those answers pointed me to <a href="http://steve.emxsoftware.com/NET/Overloading+the++and++operators" rel="noreferrer">Steve Eichert article</a> and it pointed me to <a href="http://msdn.microsoft.com/en-us/library/aa691312.aspx" rel="noreferrer">msdn</a>:</p>\n\n<blockquote>\n  <p>The operation x &amp;&amp; y is evaluated as T.false(x) ? x : T.&amp;(x, y), where T.false(x) is an invocation of the operator false declared in T, and T.&amp;(x, y) is an invocation of the selected operator &amp;. In other words, x is first evaluated and operator false is invoked on the result to determine if x is definitely false. Then, if x is definitely false, the result of the operation is the value previously computed for x. Otherwise, y is evaluated, and the selected operator &amp; is invoked on the value previously computed for x and the value computed for y to produce the result of the operation.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/33265', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2361/']